Pablo Gûereca
Pablo Gûereca

Reputation: 725

String date format convert to date

I need to convert this string to yyyy-MM-dd date format:

December 31, 2014 to 2014-12-31
May 31 , 2018 to 2018-05-31

Any suggestion? Regards!

Upvotes: 2

Views: 130

Answers (2)

Suraj Kumar
Suraj Kumar

Reputation: 5653

You can also use as shown Here

Select cast('December 31, 2014' as date) as [Date]
Select cast('December 31, 2014' as date) as [Date]

or in higher version of SQL Server provide format string value for different date format as you want.

SELECT FORMAT(cast('December 31, 2014' as Date),'yyyy-MM-dd','en-US') AS[DATE IN US FORMAT]

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1270893

You can just use convert():

select convert(date, 'December 31, 2014')

SQL Server is pretty good about doing such conversions.

So this works in both cases:

select convert(date, datestr)
from (values ('December 31, 2014'), ('May 31 , 2018')) v(datestr);

Upvotes: 5

Related Questions