Kelvin Okeyo
Kelvin Okeyo

Reputation: 11

T-SQL computed date column

I have a table, in TSQL, with a field containing data in YYYYMMDD format saved as varchar(50);

I want to add a date type column to the table for each of the corresponding records in this field. Any ideas?

Upvotes: 1

Views: 866

Answers (1)

Galma88
Galma88

Reputation: 2546

Assuming that you have stored correct format of date in your field (eg you don't have '20121433'), this script should works for you:

ALTER TABLE your_table
ADD your_field_Date DATETIME

UPDATE your_table 
SET  your_field_Date = CONVERT(DATETIME, your_field_varchar, 112)

ALTER TABLE your_table DROP COLUMN your_field_varchar

Upvotes: 1

Related Questions