Skip to content
Advertisement

How to convert char data type to date in SQL Server?

In a table, there is a column BeginDate of type char(10) and all the records are saved in 2020/05/06 format.

How can I change the data type to Date?

Advertisement

Answer

You could strip the / characters and then you have the ISO format yyyyMMdd.:

TRY_CONVERT(date,REPLACE(StringDate,'/',''),112)

But the real solution is fix your design; stop storing dates as a varchar.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement