Best approach to remove time part of datetime in SQL Server

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

    I think you mean cast(floor(cast(getdate()as float))as datetime)

    real is only 32-bits, and could lose some information

    This is fastest cast(cast(getdate()+x-0.5 as int)as datetime)

    ...though only about 10% faster(about 0.49 microseconds CPU vs. 0.58)

    This was recommended, and takes the same time in my test just now: DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)

    In SQL 2008, the SQL CLR function is about 5 times faster than using a SQL function would be, at 1.35 microseconds versus 6.5 microsections, indicating much lower function-call overhead for a SQL CLR function versus a simple SQL UDF.

    In SQL 2005, the SQL CLR function is 16 times faster, per my testing, versus this slow function:

    create function dateonly (  @dt datetime )
    returns datetime
    as
    begin
    return cast(floor(cast(@dt as float))as int)
    end
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 23:29

    In SQL Server 2008, there is a DATE datetype (also a TIME datatype).

    CAST(GetDate() as DATE)
    

    or

    declare @Dt as DATE = GetDate()
    
    0 讨论(0)
  • 2020-11-21 23:29

    BEWARE!

    Method a) and b) does NOT always have the same output!

    select DATEADD(dd, DATEDIFF(dd, 0, '2013-12-31 23:59:59.999'), 0)
    

    Output: 2014-01-01 00:00:00.000

    select cast(convert(char(11), '2013-12-31 23:59:59.999', 113) as datetime)
    

    Output: 2013-12-31 00:00:00.000

    (Tested on MS SQL Server 2005 and 2008 R2)

    EDIT: According to Adam's comment, this cannot happen if you read the date value from the table, but it can happen if you provide your date value as a literal (example: as a parameter of a stored procedure called via ADO.NET).

    0 讨论(0)
  • 2020-11-21 23:29

    Strip time on inserts/updates in the first place. As for on-the-fly conversion, nothing can beat a user-defined function maintanability-wise:

    select date_only(dd)
    

    The implementation of date_only can be anything you like - now it's abstracted away and calling code is much much cleaner.

    0 讨论(0)
提交回复
热议问题