Functions

Built-in SPARQL functions and Poliglot's registered extensions.

Functions in SPARQL are used inside FILTER and BIND expressions to transform values, test conditions, and compute results.

String Functions

CONCAT("hello", " ", "world")      # "hello world"
STR(?resource)                       # URI to string
STRLEN(?title)                       # String length
SUBSTR(?title, 1, 10)               # Substring (1-indexed)
UCASE(?name)                        # "ALICE"
LCASE(?name)                        # "alice"
CONTAINS(?title, "Q3")              # true/false
STRSTARTS(?code, "TASK-")           # true/false
STRENDS(?email, "@example.org")     # true/false
REPLACE(?phone, "-", "")            # Remove dashes

Numeric Functions

ABS(-5)                              # 5
ROUND(3.7)                          # 4
CEIL(3.2)                           # 4
FLOOR(3.9)                          # 3

Date/Time Functions

NOW()                                # Current datetime
YEAR(?date)                          # Extract year
MONTH(?date)                         # Extract month
DAY(?date)                           # Extract day

Type Functions

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

DATATYPE(?literal)                   # Get the XSD type
LANG(?label)                         # Get language tag
URI("https://example.org")          # Construct a URI from string
IRI("https://example.org")          # Same as URI
STRDT("42", xsd:integer)            # Cast string to typed literal
STRLANG("hello", "en")              # Add language tag

Logic Functions

IF(?priority = "high", 1, 0)        # Conditional
COALESCE(?nickname, ?name, "Unknown") # First non-null
BOUND(?variable)                     # Is the variable bound?
isURI(?thing)                        # Is it a URI?
isLiteral(?thing)                    # Is it a literal?
isBlank(?thing)                      # Is it a blank node?
REGEX(?name, "^A.*", "i")           # Regex test

Aggregate Functions

Used with GROUP BY:

COUNT(?task)                         # Number of matches
SUM(?amount)                         # Total
AVG(?score)                          # Average
MIN(?date)                           # Earliest
MAX(?date)                           # Latest
GROUP_CONCAT(?tag; separator=", ")   # Concatenate grouped values

Poliglot Extensions

Poliglot registers additional functions callable in BIND expressions.

rars-os:newGraph()

Creates a new temporary named graph and returns its URI. Automatically grants read/write permissions to the caller's roles.

PREFIX rars-os: <https://poliglot.io/rars/spec/os#>

BIND(rars-os:newGraph() AS ?g)

rars-os:qname()

Converts a full URI to its qname representation using the dataset's prefix mappings:

PREFIX rars-os: <https://poliglot.io/rars/spec/os#>

BIND(rars-os:qname(?actionUri) AS ?qname)
# <https://poliglot.io/rars/spec/os#Plan> -> "rars-os:Plan"

rars-os:collect()

Collects all values of a variable across result rows into an rdf:List:

PREFIX people: <https://example.org/people/>
PREFIX rars-os:     <https://poliglot.io/rars/spec/os#>

SELECT ?people WHERE {
    ?person a people:Person .
    BIND(rars-os:collect(?person) AS ?people)
}

See Also

On this page