Expressions

Date Functions

Extract components from dates, perform temporal calculations, and filter data by time periods.

Date Function Reference

SPARQL provides functions to work with xsd:dateTime and xsd:date values.

Function Description Example
YEAR(date) Extract the year as integer YEAR("2024-03-15"^^xsd:date) → 2024
MONTH(date) Extract the month (1-12) MONTH("2024-03-15"^^xsd:date) → 3
DAY(date) Extract the day of month (1-31) DAY("2024-03-15"^^xsd:date) → 15
HOURS(datetime) Extract hours (0-23) HOURS("2024-03-15T14:30:00"^^xsd:dateTime) → 14
MINUTES(datetime) Extract minutes (0-59) MINUTES("2024-03-15T14:30:00"^^xsd:dateTime) → 30
SECONDS(datetime) Extract seconds (0-59) SECONDS("2024-03-15T14:30:45"^^xsd:dateTime) → 45
NOW() Current date and time YEAR(NOW()) → current year

Extracting Date Components

Use date functions to extract year, month, and day from date properties.

Scientists Born in a Specific Year

Find Nobel Prize winners born in a particular year:

Nobel Laureates Born in 1879
Run
SELECT ?person ?personLabel ?birthDate WHERE {
  ?person wdt:P166 wd:Q7191 # Nobel Prize winner
  ?person wdt:P569 ?birthDate .
  FILTER(YEAR(?birthDate) = 1879)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ?birthDate

People Born on a Specific Day

Find notable people born on a particular date (month and day):

Famous People Born on March 14 (Pi Day)
Run
SELECT ?person ?personLabel ?birthDate ?birthYear WHERE {
  ?person wdt:P31 wd:Q5 .          # human
  ?person wdt:P569 ?birthDate .
  ?person wdt:P106 ?occupation .  # has occupation
  FILTER(MONTH(?birthDate) = 3 && DAY(?birthDate) = 14)
  BIND(YEAR(?birthDate) AS ?birthYear)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?birthYear)
LIMIT 50

Date Comparisons

Compare dates to filter by time periods or find items within date ranges.

Events in a Date Range

Find events that occurred between two dates:

Space Missions Launched in the 1960s
Run
SELECT ?mission ?missionLabel ?launchDate WHERE {
  ?mission wdt:P31/wdt:P279* wd:Q5916 .  # spaceflight
  ?mission wdt:P619 ?launchDate .         # launch date
  FILTER(?launchDate >= "1960-01-01"^^xsd:date &&
         ?launchDate < "1970-01-01"^^xsd:date)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ?launchDate
LIMIT 100

Before and After Comparisons

Find people who died before a certain age or date:

Composers Who Died Before Age 40
Run
SELECT ?composer ?composerLabel ?birthDate ?deathDate ?age WHERE {
  ?composer wdt:P106 wd:Q36834 .   # occupation: composer
  ?composer wdt:P569 ?birthDate .
  ?composer wdt:P570 ?deathDate .
  BIND(YEAR(?deathDate) - YEAR(?birthDate) AS ?age)
  FILTER(?age < 40 && ?age > 0)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ?age
LIMIT 50

Using NOW() for Current Time

The NOW() function returns the current date and time, useful for dynamic queries.

Living People Over 100

Find centenarians who are still alive:

Living Centenarians
Run
SELECT ?person ?personLabel ?birthDate ?age WHERE {
  ?person wdt:P31 wd:Q5 .           # human
  ?person wdt:P569 ?birthDate .
  FILTER NOT EXISTS { ?person wdt:P570 ?deathDate . }  # still alive
  BIND(YEAR(NOW()) - YEAR(?birthDate) AS ?age)
  FILTER(?age >= 100)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?age)
LIMIT 50

Recent Events

Find events from the past year:

Earthquakes in the Last Year
Run
SELECT ?earthquake ?earthquakeLabel ?date ?magnitude WHERE {
  ?earthquake wdt:P31 wd:Q7944 .    # earthquake
  ?earthquake wdt:P585 ?date .       # point in time
  OPTIONAL { ?earthquake wdt:P2528 ?magnitude . }
  BIND(YEAR(NOW()) - 1 AS ?lastYear)
  FILTER(YEAR(?date) >= ?lastYear)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?date)
LIMIT 50

Grouping by Date Parts

Use date extraction functions with GROUP BY for temporal aggregation.

Count by Year

Count events per year:

Movies Released Per Year (2000-2020)
Run
SELECT ?year (COUNT(?film) AS ?count) WHERE {
  ?film wdt:P31 wd:Q11424 .         # film
  ?film wdt:P577 ?pubDate .         # publication date
  BIND(YEAR(?pubDate) AS ?year)
  FILTER(?year >= 2000 && ?year <= 2020)
}
GROUP BY ?year
ORDER BY ?year

Count by Month

Find which months have the most births:

Nobel Laureate Births by Month
Run
SELECT ?month (COUNT(?person) AS ?births) WHERE {
  ?person wdt:P166 wd:Q7191 .       # Nobel Prize winner
  ?person wdt:P569 ?birthDate .
  BIND(MONTH(?birthDate) AS ?month)
}
GROUP BY ?month
ORDER BY ?month

Date Arithmetic

Calculate durations and differences between dates.

Duration Calculation

Calculate how long someone held a position:

US Presidents by Years in Office
Run
SELECT ?president ?presidentLabel ?start ?end ?yearsInOffice WHERE {
  ?president wdt:P39 wd:Q11696 .    # position held: US President
  ?president p:P39 ?statement .
  ?statement ps:P39 wd:Q11696 .
  ?statement pq:P580 ?start .       # start time
  OPTIONAL { ?statement pq:P582 ?end . }  # end time
  BIND(IF(BOUND(?end),
       YEAR(?end) - YEAR(?start),
       YEAR(NOW()) - YEAR(?start)) AS ?yearsInOffice)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?yearsInOffice)

Age at Death

Calculate precise ages:

Philosophers by Age at Death
Run
SELECT ?philosopher ?philosopherLabel ?birthDate ?deathDate ?age WHERE {
  ?philosopher wdt:P106 wd:Q4964182 .  # occupation: philosopher
  ?philosopher wdt:P569 ?birthDate .
  ?philosopher wdt:P570 ?deathDate .
  BIND(YEAR(?deathDate) - YEAR(?birthDate) AS ?age)
  FILTER(?age > 0)  # filter invalid dates
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?age)
LIMIT 50

Combined Date Filters

Combine multiple date conditions for complex temporal queries.

People Born and Died in Same Century

Find people whose entire life was within one century:

Writers Who Lived Entirely in the 19th Century
Run
SELECT ?writer ?writerLabel ?birthDate ?deathDate WHERE {
  ?writer wdt:P106 wd:Q36180 .      # occupation: writer
  ?writer wdt:P569 ?birthDate .
  ?writer wdt:P570 ?deathDate .
  FILTER(YEAR(?birthDate) >= 1801 && YEAR(?birthDate) <= 1900)
  FILTER(YEAR(?deathDate) >= 1801 && YEAR(?deathDate) <= 1900)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ?birthDate
LIMIT 100