DATEDIFF四舍五入到最近的30分钟

时间:2012-03-15 19:36:15

标签: sql sql-server sql-server-2008

我正在使用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]

我不明白我从这句话中得到的行为,但我需要的是>= :30, round up, if not, round down,

问题如何在不到30分钟时将其向下舍入,如果大于或等于30分钟则将时间向上舍入?

1 个答案:

答案 0 :(得分:3)

您应该在几分钟内计算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]