我的 SPARQL 查詢中有一個日期時間,我想將其轉換為日期。因此我這樣做:
BIND(CONCAT(YEAR(?dateTime), "-",MONTH(?dateTime), "-", DAY(?dateTime)) as ?date)
這部分代碼有效,但回傳例如 2022-2-3,我希望它是 2022-02-03。如果 dateTime 是 2022-11-23,則什么都不會改變。
uj5u.com熱心網友回復:
您可以從 、 和 函式中獲取整數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)
}
uj5u.com熱心網友回復:
@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-次
例如:
fn:format-number(1,"000")回傳字串“001”。
Apache Jena 還具有afn:sprintf使用 C 或 Java 語法的本地擴展sprintf:
afn:sprintf("d", 1)回傳“001”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/478145.html
上一篇:如果兩個日期之間的條件按小時
