Temporal
Working with Dates
Extract and filter date components using SPARQL's built-in date functions. Learn YEAR(), MONTH(), DAY() and date comparisons.
Date Functions in SPARQL
SPARQL provides several functions to extract components from datetime values. These are essential for filtering and grouping temporal data.
| Function | Returns | Example |
|---|---|---|
YEAR(?date) |
Year as integer | 2023 |
MONTH(?date) |
Month (1-12) | 7 |
DAY(?date) |
Day (1-31) | 15 |
HOURS(?datetime) |
Hour (0-23) | 14 |
MINUTES(?datetime) |
Minutes (0-59) | 30 |
SECONDS(?datetime) |
Seconds (0-59) | 45 |
Programming Languages: Filter by Year
Languages Created After 2010
SELECT ?lang ?langLabel ?inception (YEAR(?inception) AS ?year)
WHERE {
?lang wdt:P31 wd:Q9143 ;
wdt:P571 ?inception .
FILTER(YEAR(?inception) > 2010)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY DESC(?year)
LIMIT 50
Languages by Decade of Creation
SELECT ?decade (COUNT(?lang) AS ?count)
WHERE {
?lang wdt:P31 wd:Q9143 ;
wdt:P571 ?inception .
BIND(FLOOR(YEAR(?inception) / 10) * 10 AS ?decade)
}
GROUP BY ?decade
ORDER BY ?decade
Cultural Heritage: Date Ranges
Artworks Created in the 19th Century
SELECT ?artwork ?artworkLabel ?creator ?creatorLabel ?date
WHERE {
?artwork wdt:P31 wd:Q3305213 ; # painting
wdt:P170 ?creator ; # creator
wdt:P571 ?date . # inception
# 19th century: 1801-1900
FILTER(YEAR(?date) >= 1801 && YEAR(?date) <= 1900)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY ?date
LIMIT 100
Museums Founded by Month
SELECT ?month (COUNT(?museum) AS ?count)
WHERE {
?museum wdt:P31/wdt:P279* wd:Q33506 ; # museum
wdt:P571 ?inception .
BIND(MONTH(?inception) AS ?month)
FILTER(BOUND(?month)) # Only dates with month precision
}
GROUP BY ?month
ORDER BY ?month
Urban: Comparing Dates
Buildings Completed Before City Founded
SELECT ?building ?buildingLabel ?buildingDate
?city ?cityLabel ?cityFounded
WHERE {
?building wdt:P31/wdt:P279* wd:Q41176 ; # building
wdt:P571 ?buildingDate ;
wdt:P131 ?city .
?city wdt:P31/wdt:P279* wd:Q515 ;
wdt:P571 ?cityFounded .
# Building older than city
FILTER(?buildingDate < ?cityFounded)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 50
Metro Systems by Opening Year
SELECT ?metro ?metroLabel ?city ?cityLabel
(YEAR(?opened) AS ?year)
WHERE {
?metro wdt:P31 wd:Q1412403 ; # rapid transit system
wdt:P131 ?city ;
wdt:P1619 ?opened . # date of official opening
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY ?year
LIMIT 50
Date Arithmetic
SPARQL allows basic date comparisons and calculations. You can compute differences, check ranges, and sort chronologically.
Calculate Age of Programming Languages
SELECT ?lang ?langLabel (YEAR(?inception) AS ?created)
(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 20