我在SPARQL查询中有一个日期时间,我想将它转换为一个日期。因此,我要:
BIND(CONCAT(YEAR(?dateTime), "-",MONTH(?dateTime), "-", DAY(?dateTime)) as ?date) 这部分代码工作,但返回例如2022-2-3,我希望它是2022-02-03。如果dateTime是2022-11-23,什么都不应该改变.
发布于 2022-05-18 20:10:54
您可以从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)
}发布于 2022-05-18 20:43:22
@gregory-williams给出了一个便携的答案。另一种方法是F&O (XPath和XQuery函数和运算符3.1)中的函数“fn:format-.”
我不确定各种triplestores中的内容- Apache提供了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")返回字符串"001“。
Apache还有一个本地扩展afn:sprintf,它使用sprintf的C或sprintf语法。
afn:sprintf("%03d", 1)返回"001“。
https://stackoverflow.com/questions/72286700
复制相似问题