Chis Chis - 5 months ago 16
SQL Question

Convert from VARCHAR to DATETIME

I need to convert all values in a table and I need that change to be permanent so the table has dates and they are formatted like

dd.mm.yyy
.

I need to change them to
DATETIME
Format which is
yyyy-mm-dd
.

Answer

If your dates are stored with the appropriate data type (date, datetime or datetime2), then they are stored without any display format.

If that is the case you need to convert them to char(10) when selecting the data:

SELECT CONVERT(char(10), DateTimeColumn, 120)

If they are stored as a string type, you should refactor your database and store them properly. If that's impossible for some reason, you can convert twice. one from string to date, and the other one back to string:

SELECT CONVERT(char(10), CONVERT(date, DateTimeStringColumn, 104), 120)
Comments