Best approach to remove time part of datetime in SQL Server

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

    I would use:

    CAST
    (
    CAST(YEAR(DATEFIELD) as varchar(4)) + '/' CAST(MM(DATEFIELD) as varchar(2)) + '/' CAST(DD(DATEFIELD) as varchar(2)) as datetime
    ) 
    

    Thus effectively creating a new field from the date field you already have.

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

    How about select cast(cast my_datetime_field as date) as datetime)? This results in the same date, with the time set to 00:00, but avoids any conversion to text and also avoids any explicit numeric rounding.

    0 讨论(0)
  • 2020-11-21 23:23
    SELECT CAST(FLOOR(CAST(getdate() AS FLOAT)) AS DATETIME)
    

    ...is not a good solution, per the comments below.

    I would delete this answer, but I'll leave it here as a counter-example since I think the commenters' explanation of why it's not a good idea is still useful.

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

    I really like:

    [date] = CONVERT(VARCHAR(10), GETDATE(), 120)
    

    The 120 format code will coerce the date into the ISO 8601 standard:

    'YYYY-MM-DD' or '2017-01-09'
    

    Super easy to use in dplyr (R) and pandas (Python)!

    0 讨论(0)
  • 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))
    
    0 讨论(0)
  • 2020-11-21 23:24

    I, personally, almost always use User Defined functions for this if dealing with SQL Server 2005 (or lower version), however, it should be noted that there are specific drawbacks to using UDF's, especially if applying them to WHERE clauses (see below and the comments on this answer for further details). If using SQL Server 2008 (or higher) - see below.

    In fact, for most databases that I create, I add these UDF's in right near the start since I know there's a 99% chance I'm going to need them sooner or later.

    I create one for "date only" & "time only" (although the "date only" one is by far the most used of the two).

    Here's some links to a variety of date-related UDF's:

    Essential SQL Server Date, Time and DateTime Functions
    Get Date Only Function

    That last link shows no less than 3 different ways to getting the date only part of a datetime field and mentions some pros and cons of each approach.

    If using a UDF, it should be noted that you should try to avoid using the UDF as part of a WHERE clause in a query as this will greatly hinder performance of the query. The main reason for this is that using a UDF in a WHERE clause renders that clause as non-sargable, which means that SQL Server can no longer use an index with that clause in order to improve the speed of query execution. With reference to my own usage of UDF's, I'll frequently use the "raw" date column within the WHERE clause, but apply the UDF to the SELECTed column. In this way, the UDF is only applied to the filtered result-set and not every row of the table as part of the filter.

    Of course, the absolute best approach for this is to use SQL Server 2008 (or higher) and separate out your dates and times, as the SQL Server database engine is then natively providing the individual date and time components, and can efficiently query these independently without the need for a UDF or other mechanism to extract either the date or time part from a composite datetime type.

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