哆啦的时光机
关键是使用DATEADD和DATEDIFF以及适当的SQL时间间隔枚举。declare @datetime datetime;set @datetime = getdate();select @datetime;select dateadd(year,datediff(year,0,@datetime),0);select dateadd(month,datediff(month,0,@datetime),0);select dateadd(day,datediff(day,0,@datetime),0);select dateadd(hour,datediff(hour,0,@datetime),0);select dateadd(minute,datediff(minute,0,@datetime),0);select dateadd(second,datediff(second,'2000-01-01',@datetime),'2000-01-01');select dateadd(week,datediff(week,0,@datetime),-1); --Beginning of week is Sundayselect dateadd(week,datediff(week,0,@datetime),0); --Beginning of week is Monday请注意,当您按秒进行平铺时,如果使用0,则通常会出现算术溢出。因此,请选择一个保证低于您尝试放置的日期时间的已知值。