问题
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