Sparql如何制作日/月总是返回2位数字

发布于 2025-01-30 12:01:57 字数 231 浏览 3 评论 0原文

我的sparql Query中有一个日期,我想转换为日期。 因此,我确实:

 BIND(CONCAT(YEAR(?dateTime), "-",MONTH(?dateTime), "-", DAY(?dateTime)) as ?date)  

这部分代码有效,但返回例如2022-2-3,我希望它是2022-02-03。如果DateTime是2022-11-23,则什么都不应改变。

I have a datetime in my SPARQL-query that I want to transform to a date.
Therefore I do:

 BIND(CONCAT(YEAR(?dateTime), "-",MONTH(?dateTime), "-", DAY(?dateTime)) as ?date)  

This part of code works but returns for example 2022-2-3, I want it to be 2022-02-03. If the dateTime is 2022-11-23, nothing should change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小鸟爱天空丶 2025-02-06 12:01:57

您可以从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)
}

You can take the integers you get back from the YEAR, MONTH, and DAY functions and pad them with the appropriate number of zeros (after turning them into strings):

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)
}
仅此而已 2025-02-06 12:01:57

@gregory-williams 给出便携式答案。另一种替代方案是f&amp; o(xpath和xquery函数和运算符3.1)“ fn:格式-J。”

我不确定各种三分之一的覆盖范围 - apache jena jena提供fn:format-number number,这是问题所需的,但不是fn-format-datetime


https://www.w.org/www.org/tr/xpath -Functions-3/#格式 - 纳姆
https://www.w.org/tr /xpath-functions-3/#formatting-dates-and-times

例如:

fn:format-number(1,“ 000”)返回字符串“ 001”。

Apache Jena还使用Sprintf的C或Java语法:

afn:afn:sprintf(“%03D”,​​1) sprintf afn:sprintf >返回“ 001”。

@gregory-williams gives a portable answer. An alternative is functions from F&O (XPath and XQuery Functions and Operators 3.1) "fn:format-...."

I'm not sure of the coverage in various triplestores - Apache Jena provides fn:format-number, which is needed for the question, but not fn-format-dateTime etc

See
https://www.w3.org/TR/xpath-functions-3/#formatting-the-number
https://www.w3.org/TR/xpath-functions-3/#formatting-dates-and-times

For example:

fn:format-number(1,"000") returns the string "001".

Apache Jena also has a local extension afn:sprintf using the C or Java syntax of sprintf:

afn:sprintf("%03d", 1) returns "001".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文