Convert a SQL Server datetime to a shorter date format

Sql ServerTsqlDatetime

Sql Server Problem Overview


I have a datetime column in SQL Server that gives me data like this 10/27/2010 12:57:49 pm and I want to query this column but just have SQL Server return the day month and year - eg. 2010 10 27 or something like that.

What are the functions I should be researching?

Should I be trying to convert to another date data type? Or simply convert it to a string?

Sql Server Solutions


Solution 1 - Sql Server

Have a look at CONVERT. The 3rd parameter is the date time style you want to convert to.

e.g.

SELECT CONVERT(VARCHAR(10), GETDATE(), 103) -- dd/MM/yyyy format

Solution 2 - Sql Server

Try this:

print cast(getdate() as date )

Solution 3 - Sql Server

If you need the result in a date format you can use:

Select Convert(DateTime, Convert(VarChar, GetDate(), 101))

Solution 4 - Sql Server

In addition to CAST and CONVERT, if you are using Sql Server 2008, you can convert to a date type (or use that type to start with), and then optionally convert again to a varchar:

declare @myDate date
set @myDate = getdate()
print cast(@myDate as varchar(10))

output:

2012-01-17

Solution 5 - Sql Server

If you have a datetime field that gives the results like this 2018-03-30 08:43:28.177

Proposed: and you want to change the datetime to date to appear like 2018-03-30

cast(YourDateField as Date)

Solution 6 - Sql Server

With SQL Server 2005, I would use this:

select replace(convert(char(10),getdate(),102),'.',' ')

>Results: 2015 03 05

Solution 7 - Sql Server

The shortest date format of mm/dd/yy can be obtained with:

Select Convert(varchar(8),getdate(),1)

Solution 8 - Sql Server

Just add date keyword. E.g. select date(orderdate),count(1) from orders where orderdate > '2014-10-01' group by date(orderdate);

orderdate is in date time. This query will show the orders for that date rather than datetime.

Date keyword applied on a datetime column will change it to short date.

Solution 9 - Sql Server

For any versions of SQL Server: dateadd(dd, datediff(dd, 0, getdate()), 0)

Solution 10 - Sql Server

The original DateTime field : [_Date_Time]

The converted to Shortdate : 'Short_Date'

CONVERT(date, [_Date_Time]) AS 'Short_Date'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkaesView Question on Stackoverflow
Solution 1 - Sql ServerAdaTheDevView Answer on Stackoverflow
Solution 2 - Sql Serverjabu.hlongView Answer on Stackoverflow
Solution 3 - Sql ServerIsrael MarguliesView Answer on Stackoverflow
Solution 4 - Sql ServerCD JorgensenView Answer on Stackoverflow
Solution 5 - Sql ServerBIReportGuyView Answer on Stackoverflow
Solution 6 - Sql ServerAGuestView Answer on Stackoverflow
Solution 7 - Sql ServerbsivelView Answer on Stackoverflow
Solution 8 - Sql ServerAnuj KaulView Answer on Stackoverflow
Solution 9 - Sql ServerAndreyView Answer on Stackoverflow
Solution 10 - Sql ServerYousef AlbakoushView Answer on Stackoverflow