Best approach to remove time part of datetime in SQL Server

前端 未结 23 1673
南旧
南旧 2020-11-21 22:51

Which method provides the best performance when removing the time portion from a datetime field in SQL Server?

a) select DATEADD(dd, DATEDIFF(dd, 0, getdate(         


        
23条回答
  •  别那么骄傲
    2020-11-21 23:23

    I think that if you stick strictly with TSQL that this is the fastest way to truncate the time:

     select convert(datetime,convert(int,convert(float,[Modified])))
    

    I found this truncation method to be about 5% faster than the DateAdd method. And this can be easily modified to round to the nearest day like this:

    select convert(datetime,ROUND(convert(float,[Modified]),0))
    

提交回复
热议问题