Best approach to remove time part of datetime in SQL Server

前端 未结 23 1614
南旧
南旧 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:27

    Here's yet another answer, from another duplicate question:

    SELECT CAST(CAST(getutcdate() - 0.50000004 AS int) AS datetime) 
    

    This magic number method performs slightly faster than the DATEADD method. (It looks like ~10%)

    The CPU Time on several rounds of a million records:

    DATEADD   MAGIC FLOAT
    500       453
    453       360
    375       375
    406       360
    

    But note that these numbers are possibly irrelevant because they are already VERY fast. Unless I had record sets of 100,000 or more, I couldn't even get the CPU Time to read above zero.

    Considering the fact that DateAdd is meant for this purpose and is more robust, I'd say use DateAdd.

提交回复
热议问题