%META:TOPICPARENT{name="VirtTipsAndTricksSPARQL11FeaturesExamplesCollection"}% ---+ Virtuoso SPARQL 1.1. Built-In Functions Usage Examples %TOC% ---++What? This guide contains Virtuoso SPARQL 1.1. Built-In Functions Usage Examples Usage example queries which you can run against any SPARQL endpoint that supports SPARQL 1.1 and the ability to allow a verified user perform INSERT operations. ---++Why? Using built-In functions provides flexibility to build accurate and complex queries. ---++How? Here are examples showcasing Virtuoso's support for this functionality: ---+++Functions on RDF Terms * isNumeric: [[http://bit.ly/X5ZawI][See live results]] SELECT * WHERE { ?x ?t ?o . FILTER isNumeric(?o) } LIMIT 10 ---+++Functions on Strings * STRDT: [[http://bit.ly/XPpVGs][See live results]] SELECT ?o, STRDT("123", xsd:integer) WHERE { ?x ?t ?o . } LIMIT 10 * STRLANG: [[http://bit.ly/VmEenq][See live results]] SELECT ?o, STRLANG("chat", "en") WHERE { ?x ?t ?o . } LIMIT 10 * STRLEN: [[http://bit.ly/14qoF1P][See live results]] SELECT ?o, STRLEN(?t) WHERE { ?x ?t ?o . } LIMIT 10 * SUBSTR: [[http://bit.ly/1Nz9PfV][See live results]] **** SELECT DISTINCT ?name1 WHERE { ?x ?name1 . ?x ?mbox1 . ?y ?name2 . ?y ?mbox2 . FILTER (?mbox1 = ?mbox2 && ?name1 != ?name2) . FILTER( bif:length(str(?name1)) > 2 && SUBSTR(?name1, 1) LIKE "%Kingsley%" ) } LIMIT 10 * UCASE: [[http://bit.ly/1JAMSaT][See live results]] SELECT UCASE(?name1 ) WHERE { ?x ?name1 . } LIMIT 10 * LCASE: [[http://bit.ly/1GQLtbS1][See live results]] SELECT LCASE(?name1 ) WHERE { ?x ?name1 . } LIMIT 10 * strStarts: [[http://bit.ly/1Hy8gOu][See live results]] SELECT ?name1 WHERE { ?x ?name1 . FILTER ( strStarts(?name1, "Kingsley") ). } LIMIT 10 * strEnds: [[http://bit.ly/1FUNDGB][See live results]] PREFIX foaf: SELECT distinct ?name1 WHERE { ?x foaf:name ?name1 . ?x foaf:mbox ?mbox1 . FILTER ( strEnds(?name1, "hen") ). } LIMIT 10 * encode_for_uri: [[http://bit.ly/1JALipk][See live results]] SELECT encode_for_uri(?name1) as ?name WHERE { ?x ?name1 . FILTER ( bif:contains(?name1, "France") ). } LIMIT 10 * contains: [[http://bit.ly/1oxy85a][See live results]] SELECT * WHERE { ?s ?p ?o . FILTER (if (isliteral(?o), contains(str(?o), "Virtuoso"), false)) } LIMIT 10 * concat: [[http://bit.ly/1zkMZoY][See live results]] SELECT concat (?name1 ,?name2 ) WHERE { ?x ?name1 . ?x ?mbox1 . ?y ?name2 . ?y ?mbox2 . FILTER (?mbox1 = ?mbox2 && ?name1 != ?name2) . FILTER ( contains(?name1, "Dan") ). } LIMIT 10 * langMatches: [[http://bit.ly/1hnvTYg][See live results]] SELECT ?title WHERE { ?x ?title . FILTER langMatches( lang(?title), "EN" ) } LIMIT 10 * regex: [[http://bit.ly/Wvgz5K][See live results]] SELECT ?name WHERE { ?x ?name . FILTER regex(?name, "^ali", "i") . } LIMIT 10 ---+++Functions on Numerics * round: [[http://bit.ly/14qpOXh][See live results]] SELECT round(?o) WHERE { ?s a ?c . ?s geo:geometry ?geo . ?s ?p ?o . FILTER ( bif:isnumeric(?o) ) } LIMIT 10 * abs: [[http://bit.ly/XI03LQ][See live results]] SELECT abs (?o) ?geo WHERE { ?s a ?c . ?s geo:geometry ?geo . ?s ?p ?o . FILTER (bif:isnumeric(?o) ) } LIMIT 10 * ceil: [[http://bit.ly/117XiWS][See live results]] SELECT ceil (?o) WHERE { ?s a ?c . ?s geo:geometry ?geo . ?s ?p ?o . FILTER (bif:isnumeric(?o) ) } LIMIT 10 * floor: [[http://bit.ly/126XTNt][See live results]] SELECT floor(?o) WHERE { ?s a ?c . ?s geo:geometry ?geo . ?s ?p ?o . FILTER (bif:isnumeric(?o) ) } LIMIT 10 * rand: [[http://bit.ly/14rT5lj][See live results]] SELECT floor(?o) rand () WHERE { ?s a ?c . ?s geo:geometry ?geo . ?s ?p ?o . FILTER (bif:isnumeric(?o) ) } LIMIT 10 ---+++Functions on Dates and Times * now: [[http://bit.ly/1CWJOA4][See live results]] SELECT now() WHERE { ?s ?p ?o } LIMIT 1 * year: [[http://bit.ly/1waxzRP][See live results]] SELECT year("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) WHERE { ?s ?p ?o } LIMIT 1 * month: [[http://bit.ly/ZGV4na][See live results]] SELECT month ("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) WHERE { ?s ?p ?o } LIMIT 1 * day: [[http://bit.ly/1t5hbzU][See live results]] SELECT day("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) WHERE { ?s ?p ?o } LIMIT 1 * hours: [[http://bit.ly/1zkNi2Q][See live results]] SELECT hours("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) WHERE { ?s ?p ?o } LIMIT 1 * minutes: [[http://bit.ly/1t5hdYI][See live results]] SELECT minutes("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) WHERE { ?s ?p ?o } LIMIT 1 * seconds: [[http://bit.ly/10oe4bp][See live results]] SELECT seconds ("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) WHERE { ?s ?p ?o } LIMIT 1 * TIMEZONE: [[http://bit.ly/1tfTogz][See live results]] SELECT TIMEZONE ("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) WHERE { ?s ?p ?o } LIMIT 1 * tz: [[http://bit.ly/ZNWY5S][See live results]] SELECT tz("2011-01-10T14:45:13.815-05:00"^^xsd:dateTime) WHERE { ?s ?p ?o } LIMIT 1 ---+++Hash Functions * MD5: [[http://bit.ly/1x9GAau][See live results]] SELECT MD5 ('test') WHERE { ?s ?p ?o } LIMIT 1 * SHA1: [[http://bit.ly/1t5hpan][See live results]] SELECT SHA1 ('test') WHERE { ?s ?p ?o } LIMIT 1 * SHA256: [[http://bit.ly/1t5hqed][See live results]] SELECT SHA256 ('test') WHERE { ?s ?p ?o } LIMIT 1 * SHA384: [[http://bit.ly/1wtOddL][See live results]] SELECT SHA384('test') WHERE { ?s ?p ?o } LIMIT 1 * SHA512: [[http://bit.ly/VlnjQo][See live results]] SELECT SHA512('test') WHERE { ?s ?p ?o } LIMIT 1 ---++Related * [[http://www.w3.org/TR/sparql11-query/#SparqlOps][SPARQL 1.1. Functions Definitions]] * [[http://www.w3.org/TR/rdf-sparql-protocol/][SPARQL Protocol (HTTP based Query Service)]] * [[VirtTipsAndTricksGuide][Virtuoso Tips and Tricks Collection]] * [[VirtTipsAndTricksSPARQL11FeaturesExamplesCollection][Virtuoso SPARQL 1.1 Usage Examples Collection]] * [[http://virtuoso.openlinksw.com/tutorials/sparql/SPARQL_Tutorials_Part_9/SPARQL_Tutorials_Part_9.html][Virtuoso SPARQL 1.1 Syntax Tutorial]] * [[http://docs.openlinksw.com/virtuoso/rdfsparql.html][Virtuoso Documentation]]