简体   繁体   English

DATEDIFF舍入到最接近的30分钟

[英]DATEDIFF rounding to the nearest 30 minutes

I am using datediff to get the difference between two datetimes: 我正在使用datediff来获取两个日期时间之间的差异:

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, 我不明白我从该语句中得到的行为,但是我需要的是>= :30, round up, if not, round down,任何内容>= :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? 问题我如何在少于30分钟的时间内将其四舍五入,而在大于或等于30分钟的时间内如何将其四舍五入?

You should calculate the DATEDIFF in minutes and do a ROUND then: 您应该以分钟为单位计算DATEDIFF并进行ROUND然后:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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