简体   繁体   中英

SQL DATEDIFF returns wrong result

My SQL query is as below,

SELECT  CAST(-1 * DATEDIFF(hour, '21-APR-2019 11:30:00 AM', '22-APR-2019 
2:15:20 PM') % 24 AS VARCHAR) 

Output of above query:-

-3 

Expected Output :-

-2

This is because:

DATEDIFF(hour, '21-APR-2019 11:30:00 AM', '22-APR-2019 
2:15:20 PM')

returns 27 and not 26 .
Change to this:

SELECT  CAST(-1 * (DATEDIFF(minute, '21-APR-2019 11:30:00 AM', '22-APR-2019 
2:15:20 PM') / 60) % 24 AS VARCHAR)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM