SPARQL functions reference

This section lists all supported SPARQL functions in GraphDB. The function specifications include the types of the arguments and the output. Types from XML Schema should be readily recognizable as they start with the xsd: prefix. In addition, the following more generic types are used:

rdfTerm

Any RDF value: a literal, a blank node or an IRI.

iri

An IRI.

bnode

A blank node.

literal

A literal regardless of its datatype or the presence of a language tag.

string

A plain literal or a literal with a language tag. Note that plain literals have the implicit datatype xsd:string.

numeric

A literal with a numeric XSD datatype, e.g. xsd:double and xsd:long.

variable

A SPARQL variable.

expression

A SPARQL expression that may use any constants and variables to compute a value.

SPARQL 1.1 functions

Function

Description

xsd:boolean BOUND(variable var)

Returns true if the variable var is bound to a value. Returns false otherwise. Variables with the value NaN or INF are considered bound. More

rdfTerm IF(expression e1, expression e2, expression e3)

The IF function form evaluates the first argument, interprets it as a effective boolean value, then returns the value of e2 if the EBV is true, otherwise it returns the value of e3. Only one of e2 and e3 is evaluated. If evaluating the first argument raises an error, then an error is raised for the evaluation of the IF expression. More

rdfTerm COALESCE(expression e1, )

The COALESCE function form returns the RDF term value of the first expression that evaluates without error. In SPARQL, evaluating an unbound variable raises an error.

If none of the arguments evaluates to an RDF term, an error is raised. If no expressions are evaluated without error, an error is raised. More

xsd:boolean NOT EXISTS { pattern }

xsd:boolean EXISTS { pattern }

There is a filter operator EXISTS that takes a graph pattern. EXISTS returns true or false depending on whether the pattern matches the dataset given the bindings in the current group graph pattern, the dataset and the active graph at this point in the query evaluation. No additional binding of variables occurs. The NOT EXISTS form translates into fn:not(EXISTS{...}). More

xsd:boolean xsd:boolean left || xsd:boolean right

Returns a logical OR of left and right. Note that logical-or operates on the effective boolean value of its arguments. More

xsd:boolean xsd:boolean left && xsd:boolean right

Returns a logical AND of left and right. Note that logical-and operates on the effective boolean value of its arguments. More

xsd:boolean rdfTerm term1 = rdfTerm term2

Returns true if term1 and term2 are equal. Returns false otherwise. IRIs and blank nodes are equal if they are the same RDF term as defined in RDF Concepts. Literals are equal if they have an XSD datatype, the same language tag (if any) and their values produced by applying the lexical-to-value mapping of their datatypes are also equal. If the arguments are both literal but their datatype is not an XSD datatype an error will be produced. More

xsd:boolean sameTerm(rdfTerm term1, rdfTerm term2)

Returns true if term1 and term2 are the same RDF term as defined in RDF Concepts; returns false otherwise. More

xsd:boolean rdfTerm term IN (expression e1, )

The IN operator tests whether the RDF term on the left-hand side is found in the values of list of expressions on the right-hand side. The test is done with = operator, which compares the RDF term to each expression for equality. More

xsd:boolean rdfTerm term NOT IN (expression e1, )

The NOT IN operator tests whether the RDF term on the left-hand side is not found in the values of list of expressions on the right-hand side. The test is done with != operator, which compares the RDF term to each expression for inequality. More

xsd:boolean isIRI(rdfTerm term)

xsd:boolean isURI(rdfTerm term)

Returns true if term is an IRI. Returns false otherwise. More

xsd:boolean isBlank(rdfTerm term)

Returns true if term is a blank node. Returns false otherwise. More

xsd:boolean isLiteral(rdfTerm term)

Returns true if term is a literal. Returns false otherwise. More

xsd:boolean isNumeric(rdfTerm term)

Returns true if term is a numeric value. Returns false otherwise. A term is numeric if it has an appropriate datatype and has a valid lexical form, making it a valid argument to functions and operators taking numeric arguments. More

xsd:string STR(literal ltrl)

xsd:string STR(iri rsrc)

Returns the lexical form of ltrl (a literal); returns the codepoint representation of rsrc (an IRI). This is useful for examining parts of an IRI, for instance, the hostname. More

xsd:string LANG(literal ltrl)

Returns the language tag of the literal ltrl, if it has one. It returns “” if ltrl has no language tag. Note that the RDF data model does not include literals with an empty language tag. More

iri DATATYPE(literal ltrl)

Returns the datatype IRI of the literal ltrl. More

iri IRI(string str)

iri IRI(iri rsrc)

iri URI(string str)

iri URI(iri rsrc)

The IRI function constructs an IRI by resolving the string argument str. The IRI is resolved against the base IRI of the query and must result in an absolute IRI. If the function is passed an IRI rsrc, it returns the IRI unchanged. More

bnode BNODE()

bnode BNODE(string str)

The BNODE function constructs a blank node that is distinct from all blank nodes in the dataset being queried and distinct from all blank nodes created by calls to this constructor for other query solutions. If the no argument form is used, every call results in a distinct blank node. If the form with the string str is used, every call results in distinct blank nodes for different strings, and the same blank node for calls with the same string within expressions for one solution mapping. More

iri UUID()

Return a fresh IRI from the UUID URN scheme. Each call of UUID() returns a different UUID. More

xsd:string STRUUID()

Return a string that is the scheme specific part of UUID. That is, as a string literal, the result of generating a UUID, converting to a string literal and removing the initial urn:uuid:. More

xsd:integer STRLEN(string str)

The STRLEN function corresponds to the XPath fn:string-length function and returns an xsd:integer equal to the length in characters of the lexical form of the string str. More

string SUBSTR(string source, xsd:integer startingLoc)

string SUBSTR(string source, xsd:integer startingLoc, xsd:integer length)

The SUBSTR function corresponds to the XPath fn:substring function and returns a literal of the same kind (string literal or literal with language tag) as the source input parameter but with a lexical form formed from the substring of the lexical form of the source. More

string UCASE(string str)

The UCASE function corresponds to the XPath fn:upper-case function. It returns a string literal whose lexical form is the upper case of the lexical form of the argument. More

string LCASE(string str)

The LCASE function corresponds to the XPath fn:lower-case function. It returns a string literal whose lexical form is the lower case of the lexical form of the argument. More

xsd:boolean STRSTARTS(string str1, string str2)

The STRSTARTS function corresponds to the XPath fn:starts-with function and returns true if the lexical form of str1 starts with the lexical form of str2, otherwise it returns false. More

xsd:boolean STRENDS(string str1, string str2)

The STRENDS function corresponds to the XPath fn:ends-with function and returns true if the lexical form of str1 ends with the lexical form of str2, otherwise it returns false. More

xsd:boolean CONTAINS(string str1, string str2)

The CONTAINS function corresponds to the XPath fn:contains function and returns true if the lexical form of str1 contains the lexical form of str2 as a substring. More

string STRBEFORE(string str1, string str2)

The STRBEFORE function corresponds to the XPath fn:substring-before function and returns a literal of the same kind as str1. The lexical form of the result is the substring of the lexical form of str1 that precedes the first occurrence of the lexical form of str2. If the lexical form of str2 is the empty string, this is considered to be a match and the lexical form of the result is the empty string. If there is no such occurrence, an empty string literal is returned. More

string STRAFTER(string str1, string str2)

The STRAFTER function corresponds to the XPath fn:substring-after function and returns a literal of the same kind as str1. The lexical form of the result is the substring of the lexical form of str1 that follows the first occurrence of the lexical form of str2. If the lexical form of str2 is the empty string, this is considered to be a match and the lexical form of the result is the empty string. If there is no such occurrence, an empty simple literal is returned. More

xsd:string ENCODE_FOR_URI(string str)

The ENCODE_FOR_URI function corresponds to the XPath fn:encode-for-uri function. It returns a simple literal with the lexical form obtained from the lexical form of its input after translating reserved characters according to the fn:encode-for-uri function. More

string CONCAT(string str1, )

The CONCAT function corresponds to the XPath fn:concat function. The function accepts string literals as arguments.

The lexical form of the returned literal is obtained by concatenating the lexical forms of its inputs. If all input literals are literals with identical language tag, then the returned literal is a literal with the same language tag, in all other cases, the returned literal is a simple literal. More

xsd:boolean langMatches(xsd:string languageTag, xsd:string languageRange)

Returns true if languageTag (first argument) matches languageRange (second argument). According to language tag semantics, the matching is case-insensitive. languageRange is a basic language range. For example, “en” will match any of the languageTags “en”, “EN”, “En”, “en-GB”, “en-US”, etc. A language range of “*” matches any non-empty language tag string. More

xsd:boolean REGEX(string text, xsd:string pattern)

xsd:boolean REGEX(string text, xsd:string pattern, xsd:string flags)

Invokes the XPath fn:matches function to match text against a regular expression pattern. Regular expression matching may involve the modifier flags: “i” requests case-insensitive matching. More

string REPLACE(string arg, xsd:string pattern, xsd:string replacement)

string REPLACE(string arg, xsd:string pattern, xsd:string replacement, xsd:string flags)

The REPLACE function corresponds to the XPath fn:replace function. It replaces each non-overlapping occurrence of the regular expression pattern with the replacement string. Regular expression matching may involve the modifier flags: “i” requests case-insensitive matching. More

numeric ABS(numeric num)

Returns the absolute value of num. An error is raised if the argument is not a numeric value.

This function is the same as fn:numeric-abs for terms with a datatype from the XQuery and XPath Data Model specification. More

numeric ROUND(numeric num)

Returns the number with no fractional part that is closest to num. If there are two such numbers, then the one that is closest to positive infinity is returned. An error is raised if the argument is not a numeric value.

This function is the same as fn:numeric-round for terms with a datatype from the XQuery and XPath Data Model specification. More

numeric CEIL(numeric num)

Returns the smallest (closest to negative infinity) number with no fractional part that is not less than the value of num. An error is raised if the argument is not a numeric value.

This function is the same as fn:numeric-ceil for terms with a datatype from the XQuery and XPath Data Model specification. More

numeric FLOOR(numeric num)

Returns the largest (closest to positive infinity) number with no fractional part that is not greater than the value of num. An error is raised if the argument is not a numeric value.

This function is the same as fn:numeric-floor for terms with a datatype from the XQuery and XPath Data Model specification. More

xsd:double RAND()

Returns a pseudo-random number between 0 (inclusive) and 1.0 (exclusive). Different numbers can be produced every time this function is invoked. Numbers should be produced with approximately equal probability. More

xsd:dateTime NOW()

Returns an XSD dateTime value for the current query execution. All calls to this function in any one query execution will return the same value. The exact moment returned is not specified. More

xsd:integer YEAR(xsd:dateTime arg)

Returns the year part of arg as an integer.

This function corresponds to fn:year-from-dateTime. More

xsd:integer MONTH(xsd:dateTime arg)

Returns the month part of arg as an integer.

This function corresponds to fn:month-from-dateTime. More

xsd:integer DAY(xsd:dateTime arg)

Returns the day part of arg as an integer.

This function corresponds to fn:day-from-dateTime. More

xsd:integer HOURS(xsd:dateTime arg)

Returns the hours part of arg as an integer. The value is as given in the lexical form of the XSD dateTime.

This function corresponds to fn:hours-from-dateTime. More

xsd:integer MINUTES(xsd:dateTime arg)

Returns the minutes part of the lexical form of arg. The value is as given in the lexical form of the XSD dateTime.

This function corresponds to fn:minutes-from-dateTime. More

xsd:decimal SECONDS(xsd:dateTime arg)

Returns the seconds part of the lexical form of arg.

This function corresponds to fn:seconds-from-dateTime. More

xsd:dayTimeDuration TIMEZONE(xsd:dateTime arg)

Returns the timezone part of arg as an xsd:dayTimeDuration. Raises an error if there is no timezone.

This function corresponds to fn:timezone-from-dateTime except for the treatment of literals with no timezone. More

xsd:string TZ(xsd:dateTime arg)

Returns the timezone part of arg as a simple literal. Returns the empty string if there is no timezone. More

xsd:string MD5(xsd:string arg)

Returns the MD5 checksum, as a hex digit string, calculated on the UTF-8 representation of the lexical form of the argument. More

xsd:string SHA1(xsd:string arg)

Returns the SHA1 checksum, as a hex digit string, calculated on the UTF-8 representation of the lexical form of the argument. More

xsd:string SHA256(xsd:string arg)

Returns the SHA256 checksum, as a hex digit string, calculated on the UTF-8 representation of the lexical form of the argument. More

xsd:string SHA512(xsd:string arg)

Returns the SHA512 checksum, as a hex digit string, calculated on the UTF-8 representation of the lexical form of the argument. More

SPARQL 1.1 constructor functions

Casting in SPARQL 1.1 is performed by calling a constructor function for the target type on an operand of the source type. The standard includes the following constructor functions:

Note

Note that SPARQL 1.1 does not have an xsd:date constructor. Instead, use STRDT(value,xsd:date) to attach the xsd:date datatype to the value.

Constructor function

Description

literal STRDT(xsd:string lexicalForm, iri datatypeIRI)

The STRDT function constructs a literal with lexical form and type as specified by the arguments. More

xsd:langString STRLANG(xsd:string lexicalForm, xsd:string langTag)

The STRLANG function constructs a literal with lexical form and language tag as specified by the arguments. More

xsd:integer(rdfTerm value)

Casts value to xsd:integer. More

xsd:decimal(rdfTerm value)

Casts value to xsd:decimal. More

xsd:float(rdfTerm value)

Casts value to xsd:float. More

xsd:double(rdfTerm value)

Casts value to xsd:double. More

xsd:string(rdfTerm value)

Casts value to xsd:string. More

xsd:boolean(rdfTerm value)

Casts value to xsd:boolean. More

xsd:dateTime(rdfTerm value)

Casts value to xsd:dateTime. More

xsd:nonPositiveInteger(rdfTerm value)

Casts value to xsd:nonPositiveInteger. More

xsd:negativeInteger(rdfTerm value)

Casts value to xsd:negativeInteger. More

xsd:long(rdfTerm value)

Casts value to xsd:long. More

xsd:int(rdfTerm value)

Casts value to xsd:int. More

xsd:short(rdfTerm value)

Casts value to xsd:short. More

xsd:byte(rdfTerm value)

Casts value to xsd:byte. More

xsd:nonNegativeInteger(rdfTerm value)

Casts value to xsd:nonNegativeInteger. More

xsd:unsignedLong(rdfTerm value)

Casts value to xsd:unsignedLong. More

xsd:unsignedInt(rdfTerm value)

Casts value to xsd:unsignedInt. More

xsd:unsignedShort(rdfTerm value)

Casts value to xsd:unsignedShort. More

xsd:unsignedByte(rdfTerm value)

Casts value to xsd:unsignedByte. More

xsd:positiveInteger(rdfTerm value)

Casts value to xsd:positiveInteger. More