Convert string to date in specific format

Petah picture Petah · Jul 22, 2014 · Viewed 13k times · Source

How do I convert a string to a date type in SQL Server 2008 R2?

My string is formatted dd/mm/yyyy

I tried this

SELECT CAST('01/08/2014' AS DATE)

But that does the cast in mm/dd/yyyy format.

Answer

Blorgbeard picture Blorgbeard · Jul 22, 2014

You need the convert function, where you can specify a format code:

select convert(datetime, '01/08/2014', 103)

The 103 means dd/mm/yyyy. See the docs.