Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update datetime column from varchar.

Author  Topic 

maevr
Posting Yak Master

169 Posts

Posted - 2010-12-20 : 01:41:48
I have splitted an xml to different columns to a tmp_table storing only varchar columns, the next step converts the data into different datatypes in a new ba_table. I need help update a datetime column from a varchar column where to users has stored all types of valid and invalid formats. My insert fails everytime because the format of the varchar is invalid and convert cannot continue.

How can I update this column if the values are correct and all the others to null?

Can I use a try/catch on this column alone inside the insert script to the ba_table

Preferred format: yyyy-mm-dd

My reduced insert:
insert into ba_table(id, col1, col2)
(select id, convert(datetime, col1) as col1, convert(int, col2) as col2 from tmp_table)

Kristen
Test

22859 Posts

Posted - 2010-12-20 : 03:12:10
[code]SET DATEFORMAT YMD
insert into ba_table(id, col1, col2)
(select id, CASE WHEN IsDate(col1) = 1 THEN convert(datetime, col1) ELSE NULL END as col1, convert(int, col2) as col2 from tmp_table)
[/code]
may help
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-12-20 : 05:18:57
Also make sure to read this
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-ii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -