简体   繁体   中英

DATEDIFF rounding to the nearest 30 minutes

I am using datediff to get the difference between two datetimes:

DATEDIFF(hh, CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) AS DATETIME) 
    + CAST(dbo.QuickLabDump.[Time Entered] AS TIME), 
   CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) 
    + CAST(dbo.QuickLabDump.[Time Completed] AS TIME)) AS [Hours TurnAround]

I don't understand the behavior that I am getting from this statement but what I need is anything that is >= :30, round up, if not, round down,

question how do I get it to round down when less than 30 minutes and round the hours up if greater or equal to 30 minutes?

You should calculate the DATEDIFF in minutes and do a ROUND then:

ROUND(CAST(DATEDIFF(MINUTES, CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) AS DATETIME) 
                      + CAST(dbo.QuickLabDump.[Time Entered] AS TIME), CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) 
                      + CAST(dbo.QuickLabDump.[Time Completed] AS TIME)) AS FLOAT)/60,0)  AS [Hours TurnAround]

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