How to select date without time in SQL

后端 未结 17 2026
灰色年华
灰色年华 2020-11-28 20:09

When I select date in SQL it is returned as 2011-02-25 21:17:33.933. But I need only the Date part, that is 2011-02-25. How can I do this?

相关标签:
17条回答
  • 2020-11-28 21:00

    The fastest is datediff, e.g.

    select dateadd(d, datediff(d,0, [datecolumn]), 0), other..
    from tbl
    

    But if you only need to use the value, then you can skip the dateadd, e.g.

    select ...
    WHERE somedate <= datediff(d, 0, getdate())
    

    where the expression datediff(d, 0, getdate()) is sufficient to return today's date without time portion.

    0 讨论(0)
  • 2020-11-28 21:04

    If you want to return a date type as just a date use

    CONVERT(date, SYSDATETIME())
    

    or

    SELECT CONVERT(date,SYSDATETIME()) 
    

    or

    DECLARE @DateOnly Datetime
    SET @DateOnly=CONVERT(date,SYSDATETIME())
    
    0 讨论(0)
  • 2020-11-28 21:05

    Use CAST(GETDATE() as date) that worked for me, simple.

    0 讨论(0)
  • 2020-11-28 21:05

    you can use like this

    SELECT Convert(varchar(10), GETDATE(),120) 
    
    0 讨论(0)
  • 2020-11-28 21:05

    It's a bit late, but use the ODBC "curdate" function (angle brackes 'fn' is the ODBC function escape sequence).

    SELECT {fn curdate()} 
    

    Output: 2013-02-01

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