Convert varchar to datetime in MSSQL

SQL server

A simple conversion between MsSQL types can be done with the “CONVERT” function, the first parameter is the destination type, the second is the source value:

CONVERT(datetime, ‘21-12-2011’)

It is very useful to use this conversion method  when the date is saved as a string and not as a “DATETIME”. However, we should tell the SQL DB how to parse correctly the date string.

The practice, in my opinion, would be to specify the date format right before the SQL command.

SET DATEFORMAT dmy; CONVERT(datetime, ‘21-12-2011’);

Post a Comment

Previous Post Next Post