SQL Server:Round Round Up

时间:2017-11-09 16:40:03

标签: sql sql-server database

我想将日期时间值舍入到最近的整天。

UPDATE [DB].[dbo].[TABLE]
-- I'm unsure how to take the DepartureDate and round it up
SET DepartureDate = DepartureDate + ??
WHERE DATEPART(HOUR, DepartureDate) = 23

DepartureDatedatetime列。

因此2017-11-10 23:00:00.0000变为2017:11:11 00:00:00.0000

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT DepartureDate, 
       CASE WHEN DepartureDate = CONVERT(DATE, DepartureDate) 
            THEN DepartureDate 
            ELSE CONVERT(DATE,DepartureDate + 1)
       END RoundedDate
FROM YourTable;

这会将DepartureDate向下舍入到第二天,除非DepartureDate已经正好在午夜。它正在利用date类型的默认时间为午夜这一事实。

更新只会是:

UPDATE YourTable
SET DepartureDate = CASE WHEN DepartureDate = CONVERT(DATE, DepartureDate) 
                         THEN DepartureDate 
                         ELSE CONVERT(DATE,DepartureDate + 1)
                    END;

修改由于在评论中澄清了您只想在小时= 23时执行此操作,因此只需执行此操作:

UPDATE YourTable
SET DepartureDate = CONVERT(DATE,DepartureDate +1)
WHERE DATEPART(HOUR, DepartureDate) = 23

答案 1 :(得分:0)

运行此选项,根据需要检查behviour轮次,然后应用于您的查询:

SELECT CAST(ROUND(CAST(getutcdate() as FLOAT),0) as datetime)

建议您使用旧日期和新日期选择您的数据,并首先进行眼球检查..更新前

答案 2 :(得分:0)

如果您可以使用此脚本添加日期

,那么您在where子句中是正确的
select DATEADD(DD, 1,DepartureDate )

- 更新

UPDATE [DB].[dbo].[TABLE]
SET DepartureDate = DATEADD(DD, 1,DepartureDate )
WHERE DATEPART(HOUR, DepartureDate) = 23