SPARQL 如何让 DAY/MONTH 总是 return 2 位数
SPARQL how to make DAY/MONTH always return 2 digits
我的 SPARQL 查询中有一个日期时间,我想将其转换为日期。
因此我这样做:
BIND(CONCAT(YEAR(?dateTime), "-",MONTH(?dateTime), "-", DAY(?dateTime)) as ?date)
这部分代码有效,但 returns 例如 2022-2-3,我希望它是 2022-02-03。如果日期时间是 2022 年 11 月 23 日,则不会有任何变化。
您可以获取从 YEAR
、MONTH
和 DAY
函数返回的整数,并用适当数量的零填充它们(在将它们转换为字符串之后) :
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT * WHERE {
BIND(2022 AS ?yearInt) # this would come from your YEAR(?dateTime) call
BIND(2 AS ?monthInt) # this would come from your MONTH(?dateTime) call
BIND(13 AS ?dayInt) # this would come from your DAY(?dateTime) call
# convert to strings
BIND(STR(?yearInt) AS ?year)
BIND(STR(?monthInt) AS ?month)
BIND(STR(?dayInt) AS ?day)
# pad with zeros
BIND(CONCAT("00", ?year) AS ?paddedYear)
BIND(CONCAT("0000", ?month) AS ?paddedMonth)
BIND(CONCAT("00", ?day) AS ?paddedDay)
# extract the right number of digits from the padded strings
BIND(SUBSTR(?paddedYear, STRLEN(?paddedYear)-3) AS ?fourDigitYear)
BIND(SUBSTR(?paddedDay, STRLEN(?paddedDay)-1) AS ?twoDigitDay)
BIND(SUBSTR(?paddedMonth, STRLEN(?paddedMonth)-1) AS ?twoDigitMonth)
# put it all back together
BIND(CONCAT(?fourDigitYear, "-", ?twoDigitMonth, "-", ?twoDigitDay) as ?date)
}
@gregory-williams 给出了一个可移植的答案。另一种方法是 F&O 中的函数(XPath 和 XQuery 函数和运算符 3.1)“fn:format-....”
我不确定各种三元组的覆盖范围 - Apache Jena 提供 fn:format-number
,这是问题所需要的,但不是 fn-format-dateTime
等
见
https://www.w3.org/TR/xpath-functions-3/#formatting-the-number
https://www.w3.org/TR/xpath-functions-3/#formatting-dates-and-times
例如:
fn:format-number(1,"000")
returns 字符串“001”.
Apache Jena 也有一个本地扩展 afn:sprintf
使用 sprintf
的 C 或 Java 语法:
afn:sprintf("%03d", 1)
returns "001".
我的 SPARQL 查询中有一个日期时间,我想将其转换为日期。 因此我这样做:
BIND(CONCAT(YEAR(?dateTime), "-",MONTH(?dateTime), "-", DAY(?dateTime)) as ?date)
这部分代码有效,但 returns 例如 2022-2-3,我希望它是 2022-02-03。如果日期时间是 2022 年 11 月 23 日,则不会有任何变化。
您可以获取从 YEAR
、MONTH
和 DAY
函数返回的整数,并用适当数量的零填充它们(在将它们转换为字符串之后) :
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT * WHERE {
BIND(2022 AS ?yearInt) # this would come from your YEAR(?dateTime) call
BIND(2 AS ?monthInt) # this would come from your MONTH(?dateTime) call
BIND(13 AS ?dayInt) # this would come from your DAY(?dateTime) call
# convert to strings
BIND(STR(?yearInt) AS ?year)
BIND(STR(?monthInt) AS ?month)
BIND(STR(?dayInt) AS ?day)
# pad with zeros
BIND(CONCAT("00", ?year) AS ?paddedYear)
BIND(CONCAT("0000", ?month) AS ?paddedMonth)
BIND(CONCAT("00", ?day) AS ?paddedDay)
# extract the right number of digits from the padded strings
BIND(SUBSTR(?paddedYear, STRLEN(?paddedYear)-3) AS ?fourDigitYear)
BIND(SUBSTR(?paddedDay, STRLEN(?paddedDay)-1) AS ?twoDigitDay)
BIND(SUBSTR(?paddedMonth, STRLEN(?paddedMonth)-1) AS ?twoDigitMonth)
# put it all back together
BIND(CONCAT(?fourDigitYear, "-", ?twoDigitMonth, "-", ?twoDigitDay) as ?date)
}
@gregory-williams 给出了一个可移植的答案。另一种方法是 F&O 中的函数(XPath 和 XQuery 函数和运算符 3.1)“fn:format-....”
我不确定各种三元组的覆盖范围 - Apache Jena 提供 fn:format-number
,这是问题所需要的,但不是 fn-format-dateTime
等
见
https://www.w3.org/TR/xpath-functions-3/#formatting-the-number
https://www.w3.org/TR/xpath-functions-3/#formatting-dates-and-times
例如:
fn:format-number(1,"000")
returns 字符串“001”.
Apache Jena 也有一个本地扩展 afn:sprintf
使用 sprintf
的 C 或 Java 语法:
afn:sprintf("%03d", 1)
returns "001".