# Evaluating strings in sparql

**URL:** https://community.stardog.com/t/evaluating-strings-in-sparql/335
**Category:** Support
**Created:** [May 19, 2017, 4:16pm UTC](https://community.stardog.com/t/evaluating-strings-in-sparql/335 "2017-05-19T16:16:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Samur\_Araujo](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Samur\_Araujo](https://community.stardog.com/u/Samur_Araujo)
#### Post date: [May 19, 2017, 4:16pm UTC](https://community.stardog.com/t/evaluating-strings-in-sparql/335/1 "2017-05-19T16:16:56Z")

</div>

Hi all, is it possible to evaluate a string in sparql?

Example:

```auto
select ?value where {
?s foo:frequency ?a .
?s foo:formula ?formula.

bind( eval (?formula) as ?value)
}

```

Where the ?formula would be a string like this:  
`?formula = "?a + 10"` (it is store in the database as a string)

I want to evaluate the expression `?a + 10` during execution time.

Is that functionality available?

Best,

---

<div class="post-metadata">

### Author: ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)
#### Post date: [May 19, 2017, 4:36pm UTC](https://community.stardog.com/t/evaluating-strings-in-sparql/335/2 "2017-05-19T16:36:22Z")

</div>

Out of the box you can't do something like that but Stardog makes it easy to write user defined functions that could do something like that. See [this](https://github.com/stardog-union/stardog-examples/tree/develop/examples/function) example.

Your query would need to look a little bit different like the following:

> select ?value where {  
> ?s foo:frequency ?a .  
> ?s foo:formula ?formula.

> bind( myfunc:eval (?formula, ?a) as ?value)  
> }

I was working on writing a user defined function that would execute javascript expressions that would be similar to this but haven't gotten around to finishing it. I might be motivated to finish it if it was something that you'd find useful 😉

PS I'm assuming you're looking for something generic and want to use some additional logic for selecting the formula and/or have a large number of formulas. You can evaluate basic arithmetic expressions directly in SPARQL.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)
#### Post date: [June 2, 2017, 4:36pm UTC](https://community.stardog.com/t/evaluating-strings-in-sparql/335/3 "2017-06-02T16:36:28Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
