Best approach to remove time part of datetime in SQL Server

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

    Just in case anyone is looking in here for a Sybase version since several of the versions above didn't work

    CAST(CONVERT(DATE,GETDATE(),103) AS DATETIME)
    
    • Tested in I SQL v11 running on Adaptive Server 15.7
    0 讨论(0)
  • 2020-11-21 23:04

    Here I made a function to remove some parts of a datetime for SQL Server. Usage:

    • First param is the datetime to be stripped off.
    • Second param is a char:
      • s: rounds to seconds; removes milliseconds
      • m: rounds to minutes; removes seconds and milliseconds
      • h: rounds to hours; removes minutes, seconds and milliseconds.
      • d: rounds to days; removes hours, minutes, seconds and milliseconds.
    • Returns the new datetime

    create function dbo.uf_RoundDateTime(@dt as datetime, @part as char) returns datetime as begin if CHARINDEX( @part, 'smhd',0) = 0 return @dt; return cast( Case @part when 's' then convert(varchar(19), @dt, 126) when 'm' then convert(varchar(17), @dt, 126) + '00' when 'h' then convert(varchar(14), @dt, 126) + '00:00' when 'd' then convert(varchar(14), @dt, 112) end as datetime ) end

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

    Strictly, method a is the least resource intensive:

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

    Proven less CPU intensive for same total duration a million rows by some one with way too much time on their hands: Most efficient way in SQL Server to get date from date+time?

    I saw a similar test elsewhere with similar results too.

    I prefer the DATEADD/DATEDIFF because:

    • varchar is subject to language/dateformat issues
      Example: Why is my CASE expression non-deterministic?
    • float relies on internal storage
    • it extends to work out first day of month, tomorrow etc by changing "0" base

    Edit, Oct 2011

    For SQL Server 2008+, you can CAST to date i.e. CAST(getdate() AS date). Or just use date datatype so no time to remove.

    Edit, Jan 2012

    A worked example of how flexible this is: Need to calculate by rounded time or date figure in sql server

    Edit, May 2012

    Do not use this in WHERE clauses and the like without thinking: adding a function or CAST to a column invalidates index usage. See number 2 here: http://www.simple-talk.com/sql/t-sql-programming/ten-common-sql-programming-mistakes/

    Now, this does have an example of later SQL Server optimiser versions managing CAST to date correctly, but generally it will be a bad idea ...

    Edit, Sep 2018, for datetime2

    DECLARE @datetime2value datetime2 = '02180912 11:45' --this is deliberately within datetime2, year 0218
    DECLARE @datetime2epoch datetime2 = '19000101'
    
    select DATEADD(dd, DATEDIFF(dd, @datetime2epoch, @datetime2value), @datetime2epoch)
    
    0 讨论(0)
  • 2020-11-21 23:07

    Of-course this is an old thread but to make it complete.

    From SQL 2008 you can use DATE datatype so you can simply do:

    SELECT CONVERT(DATE,GETDATE())
    
    0 讨论(0)
  • 2020-11-21 23:09

    If possible, for special things like this, I like to use CLR functions.

    In this case:

    [Microsoft.SqlServer.Server.SqlFunction]
        public static SqlDateTime DateOnly(SqlDateTime input)
        {
            if (!input.IsNull)
            {
                SqlDateTime dt = new SqlDateTime(input.Value.Year, input.Value.Month, input.Value.Day, 0, 0, 0);
    
                return dt;
            }
            else
                return SqlDateTime.Null;
        }
    
    0 讨论(0)
  • 2020-11-21 23:11

    In SQL Server 2008, you can use:

    CONVERT(DATE, getdate(), 101)
    
    0 讨论(0)
提交回复
热议问题