How to select date without time in SQL

后端 未结 17 2028
灰色年华
灰色年华 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 20:43

    I guess he wants a string.

    select convert(varchar(10), '2011-02-25 21:17:33.933', 120)
    

    120 here tells the convert function that we pass the input date in the following format: yyyy-mm-dd hh:mi:ss.

    0 讨论(0)
  • 2020-11-28 20:45

    Convert it back to datetime after converting to date in order to keep same datatime if needed

    select Convert(datetime, Convert(date, getdate())  )
    
    0 讨论(0)
  • 2020-11-28 20:46

    For 2008 older version :

    SELECT DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

    0 讨论(0)
  • 2020-11-28 20:47

    You can try this one too.

    SELECT CONVERT(DATE, GETDATE(), 120)
    
    0 讨论(0)
  • 2020-11-28 20:48

    Try this.

    SELECT DATEADD(DD, 0, DATEDIFF(DD, 0, GETDATE()))
    
    0 讨论(0)
  • 2020-11-28 20:51

    First Convert the date to float (which displays the numeric), then ROUND the numeric to 0 decimal points, then convert that to datetime.

    convert(datetime,round(convert(float,orderdate,101),0) ,101)
    
    0 讨论(0)
提交回复
热议问题