Chapter 2

Expressions

Transform and manipulate data with SPARQL's powerful expression functions. Work with strings, dates, numbers, and hashes to extract precisely the information you need.

4
Categories
30+
Functions
Intermediate
Level

Working with Expressions

SPARQL expressions allow you to transform, filter, and compute new values from your query results. Whether you need to extract a year from a date, concatenate strings, perform calculations, or generate identifiers, expression functions provide the tools you need.

Expressions are used in FILTER clauses, BIND statements, and SELECT projections to manipulate and derive new data.

Function Categories

Quick Examples

String Manipulation

Extract and Transform Text
Run ↗
SELECT ?lang ?langLabel
       (UCASE(?langLabel) AS ?upperName)
       (STRLEN(?langLabel) AS ?nameLength)
WHERE {
  ?lang wdt:P31 wd:Q9143 .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
LIMIT 10

Date Extraction

Extract Year from Date
Run ↗
SELECT ?lang ?langLabel ?inception
       (YEAR(?inception) AS ?year)
       (MONTH(?inception) AS ?month)
WHERE {
  ?lang wdt:P31 wd:Q9143 ;
        wdt:P571 ?inception .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
ORDER BY DESC(?inception)
LIMIT 10

Mathematical Calculations

Calculate Age of Programming Languages
Run ↗
SELECT ?langLabel ?inception
       (YEAR(NOW()) - YEAR(?inception) AS ?age)
WHERE {
  ?lang wdt:P31 wd:Q9143 ;
        wdt:P571 ?inception .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
ORDER BY DESC(?age)
LIMIT 10

Common Functions Reference

Function Category Description
STR() String Convert to string
CONCAT() String Concatenate strings
SUBSTR() String Extract substring
UCASE() / LCASE() String Convert case
YEAR() / MONTH() / DAY() Date Extract date components
NOW() Date Current datetime
ABS() / ROUND() / FLOOR() / CEIL() Math Numeric operations
MD5() / SHA256() Hash Generate hash

Continue Learning

📘

← Basic Queries

Review foundational SPARQL concepts.

Go Back
📊

Aggregate Queries →

Learn to analyze data with COUNT, SUM, and GROUP BY.

Continue