Best approach to remove time part of datetime in SQL Server

前端 未结 23 1464
南旧
南旧 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:13
    SELECT CAST(CAST(GETDATE() AS DATE) AS DATETIME)
    
    0 讨论(0)
  • 2020-11-21 23:13
    CAST(round(cast(getdate()as real),0,1) AS datetime)
    

    This method does not use string function. Date is basically a real datatype with digits before decimal are fraction of a day.

    this I guess will be faster than a lot.

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

    See this question:
    How can I truncate a datetime in SQL Server?

    Whatever you do, don't use the string method. That's about the worst way you could do it.

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

    For me the code below is always a winner:

    SELECT CONVERT(DATETIME, FLOOR(CONVERT(FLOAT,GETDATE())));
    
    0 讨论(0)
  • 2020-11-21 23:16

    select CONVERT(char(10), GetDate(),126)

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

    Already answered but ill throw this out there too... this suposedly also preforms well but it works by throwing away the decimal (which stores time) from the float and returning only whole part (which is date)

     CAST(
    FLOOR( CAST( GETDATE() AS FLOAT ) )
    AS DATETIME
    )
    

    second time I found this solution... i grabbed this code off

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