Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I update table column values from YYYYMMDD to YYYYMM

Account_Open_Date
19931122
20041102

So I want to convert the dates to be

Account_Open_Date
199311
200411

SELECT CAST(convert(varchar(6),a.Account_Open_Date,112) as int) FROM [tempdb].[dbo].[SQL Test Data] a WHERE Account_Open_Date is NOT NULL;

This select statement pulls up the dates how I want them but I want to update the table to the same format and am having a hard time with Updating. I don’t have a good example of what I have tried.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

update x.Account_Open_Date SET a.Account_Open_Date = CAST(convert(varchar(6),Account_Open_Date,112) as int) FROM ( [tempdb].[dbo].[SQL Test Data] ) x WHERE Account_Open_Date is NOT NULL;

>Solution :

The comments bring up excellent points. Seeing as you are doing this on a temp table I will assume you are testing and wont dive into the points brought up.

Fixed your update statement. You were close.

update x 
SET 
    x.Account_Open_Date = CAST(convert(varchar(6),Account_Open_Date,112) as int) 
FROM 
    [tempdb].[dbo].[SQL Test Data] x 
WHERE 
   Account_Open_Date is NOT NULL;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading