convert string to date in sql server

20

How do I convert YYYY-MM-DD (2012-08-17) to a date in SQL Server?

I don't see this format listed on the help page: http://msdn.microsoft.com/en-us/library/ms187928.aspx

This question is tagged with sql sql-server

~ Asked on 2012-11-07 20:29:51

The Best Answer is


27

I think style no. 111 (Japan) should work:

SELECT CONVERT(DATETIME, '2012-08-17', 111)

And if that doesn't work for some reason - you could always just strip out the dashes and then you have the totally reliable ISO-8601 format (YYYYMMDD) which works for any language and date format setting in SQL Server:

SELECT CAST(REPLACE('2012-08-17', '-', '') AS DATETIME)

~ Answered on 2012-11-07 20:32:13


6

This will do the trick:

SELECT CONVERT(char(10), GetDate(),126)

~ Answered on 2012-11-07 20:32:44


Most Viewed Questions: