sqlsql-servert-sqlsql-server-2008-r2date-parsing

Convert string to date in specific format


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.


Solution

  • 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.