Reputation: 61
I am trying to implement a SPARQL query which takes the substring from an object in my RDF data.
With the following query I get an empty result in ?Substring
column. I am getting valid output in the ?subject
, ?predicate
and ?object
columns but the ?Substring
column doesn't produce output.
SELECT * WHERE
{
?S ?P ?O .
BIND(SUBSTR(?O, 2, 3) AS ?Substring)
}
Upvotes: 4
Views: 66
Reputation: 28646
Are the values in ?O
actually string literals? If not then SUBSTR()
won't do anything since it is only defined to work on literals. If the values are URIs/Blank Nodes then you are always going to get an empty column with your query written as-is.
You can use the STR()
function to convert to a string which may cause you to start getting non-empty results like so:
SELECT * WHERE
{
?S ?P ?O .
BIND(SUBSTR(STR(?O), 2, 3) AS ?Substring)
}
In general in SPARQL if a column created from expression evaluation is empty for a given row it means that the expression evaluated to an error for that row. If you are still getting empty results then you need to look more closely at your expressions to figure out what is wrong.
Another possibility in your case is that the character range specified is not valid, note that the SPARQL SUBSTR()
function takes a start index and a length so you are asking here for 3 characters starting at index 2. If you are coming from languages like Java where the final argument is the end index and you were wanting to get the substring between the indexes 2 and 3 then what you actually need is the following:
BIND(SUBSTR(STR(?O), 2, 1)
i.e. 1 character starting at index 2
Upvotes: 3