User 23
User 23

Reputation: 163

SPARQL function to retrieve time from datetime

I have a data set stored in a RDF Store with a SPARQL endpoint. An example of data:

NUMBER       DATETIME
 "5"         "2011-05-25T12:01:35"^^xsd:dateTime
 "5"         "2011-05-25T12:01:40"^^xsd:dateTime
 "6"         "2011-05-26T12:01:35"^^xsd:dateTime

The value of column "Datetime" is of type xsd:dateTime.

I need to retrieve the time value out of the datetime, in order to make some queries. For example, if I want to retrieve all data recorded at 12:01:35 (regardless of what day it has been recorded), which SPARQL query should I use? Is there any function or similar that allows me to make this query? Or should I store the data in xsd:time format (without the date) first?

Thanks in advance,

Upvotes: 1

Views: 1693

Answers (2)

AndyS
AndyS

Reputation: 16630

If the system you are using supports it, you can use:

BIND (xsd:time(?dateTime) AS ?time)

Upvotes: 1

Henriette Harmse
Henriette Harmse

Reputation: 4762

Here is an example from the Learning SPARQL book:

PREFIX d: <http://learningsparql.com/ns/data#>
PREFIX t: <http://purl.org/tio/ns#>
SELECT ?mtg ?yearTest ?monthTest ?dayTest ?hoursTest ?minutesTest
WHERE
{
    ?mtg t:starts ?startTime .
    BIND (year(?startTime) AS ?yearTest)
    BIND (month(?startTime) AS ?monthTest)
    BIND (day(?startTime) AS ?dayTest)
    BIND (hours(?startTime) AS ?hoursTest)
    BIND (minutes(?startTime) AS ?minutesTest)
}

Upvotes: 1

Related Questions