tsql – SQL Server 2012:从时间戳列舍入到NEAREST(!)月初(以时间戳格式)

我需要从timestamp列舍入到NEAREST月初(以时间戳格式).

怎么做到这一点?

例子:

TimestampColumn A:             Rounded to these values
2012-01-07 18:18:29.923        2012-01-01 00:00:00.000
2012-01-14 12:58:13.122        2012-01-01 00:00:00.000
2012-06-09 17:10:30.787        2012-06-01 00:00:00.000
2012-05-31 09:29:43.870        2012-06-01 00:00:00.000
2012-10-22 12:09:47.067        2012-11-01 00:00:00.000
2012-10-15 04:35:11.013        2012-10-01 00:00:00.000

最佳答案 考虑先转换为日期

DECLARE @d DATETIME
set @d = CONVERT(DATE, '2012-02-14 12:58:13.122')

SET @d =  DATEADD(DAY, 1-datepart(day, @d), @d)

SELECT @d
点赞