How to return only the Date from a SQL Server Date

2018-12-31 01:54发布

SELECT GETDATE()

Returns: 2008-09-22 15:24:13.790

I want that date part without the time part: 2008-09-22 00:00:00.000

How can I get that?

30条回答
泪湿衣
2楼-- · 2018-12-31 02:32

This was missing in all answers, may not be the most efficient but very easy to write and understand, no style needed, no complex date functions.

SELECT CONVERT(DATETIME,CONVERT(DATE,((GETDATE()))))
查看更多
美炸的是我
3楼-- · 2018-12-31 02:33

For return in date format

CAST(OrderDate AS date)

The above code will work in sql server 2010

It will return like 12/12/2013

For SQL Server 2012 use the below code

CONVERT(VARCHAR(10), OrderDate , 111)
查看更多
长期被迫恋爱
4楼-- · 2018-12-31 02:34
SELECT CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))
查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 02:34
 Convert(nvarchar(10), getdate(), 101) --->  5/12/14

 Convert(nvarchar(12), getdate(), 101) --->  5/12/2014
查看更多
何处买醉
6楼-- · 2018-12-31 02:34

Okay, Though I'm bit late :), Here is the another solution.

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

Result

2008-09-22 00:00:00.000

And if you are using SQL Server 2012 and higher then you can use FORMAT() function like this -

SELECT FORMAT(GETDATE(), 'yyyy-MM-dd')
查看更多
无色无味的生活
7楼-- · 2018-12-31 02:34

Simply you can do this way:

SELECT CONVERT(date, getdate())
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @your_date))
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

Outputs as:

2008-09-22 00:00:00.000

Or simply do like this:

SELECT CONVERT (DATE, GETDATE()) 'Date Part Only'

Result:

Date Part Only
--------------
2013-07-14
查看更多
登录 后发表回答