0

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.

Matt
  • 14,906
  • 27
  • 99
  • 149
Chis
  • 131
  • 10

4 Answers4

0

Use the CONVERT function with style 20.

SELECT CONVERT(VARCHAR,yourdatefield,20)

Outputs:

yyyy-mm-dd hh:mi:ss
Matt
  • 14,906
  • 27
  • 99
  • 149
0
SELECT CONVERT(NVARCHAR(10),CONVERT(DATETIME,yourData,104),20)
ɐlǝx
  • 1,384
  • 2
  • 17
  • 22
0

If the format is "dd.mm.yyyy". Follow the steps.

  1. Add New Column

        ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATETIME/DATE;
    
  2. Update the column with date value from old column

        UPDATE TABLE_NAME SET COLUMN_NAME=CONVERT(DATETIME,V_DATE_COL,104);
    
  3. Remove Old Column

        ALTER TABLE TABLE_NAME DROP COLUMN V_DATE_COL
    
  4. Rename New Column

        EXEC sp_RENAME 'TABLE_NAME.COLUMN_NAME' , 'NewColumnName', 'COLUMN'
    
Rajesh Ranjan
  • 537
  • 2
  • 12
0

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)
Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121