What would be the XQuery expression to count the number of seconds between two dateTimes?

安稳与你 提交于 2019-12-24 10:47:13

问题


SQL query:

SELECT 
    ROUND((CAST(DATEATTRIBUTE2 AS DATE) - CAST(DATEATTRIBUTE1 AS DATE)) * 86400) AS result 
FROM TEST_TABLE;

Both DATEATTRIBUTE1 and DATEATTRIBUTE2 are of TIMESTAMP type.

I have tried and come up with the following XQuery :

fn:days-from-duration(fn:subtract-dateTimes-yielding-dayTimeDuration(
     $P_REQUEST/ns1:DATEATTRIBUTE2,$P_REQUEST/ns1:DATEATTRIBUTE1))*86400

But this fails for cases when the dates are same but there is difference in time.E.g. When DATEATTRIBUTE1 is 2017-02-23T01:17:18.0000 and DATEATTRIBUTE2 is 2017-02-23T01:17:20.7550 the SQL query returns 2 while XQuery returns 0.

Thanks in advance for the help!


回答1:


If I correctly understand, you need the total number of seconds between the two dateTimes. You can do it this way:

floor(
  ($P_REQUEST/ns1:DATEATTRIBUTE2 - $P_REQUEST/ns1:DATEATTRIBUTE1)
  div xs:dayTimeDuration('PT1S')
)

That is, substracting them -- you can use the - operator -- and then dividing the obtained duration by the duration of 1s, then rounding down.

It yields 2 for the example given in the question.

For further reference, there is a functx function documented here that suggests this way.



来源:https://stackoverflow.com/questions/42437883/what-would-be-the-xquery-expression-to-count-the-number-of-seconds-between-two-d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!