i have about 12 tables i need to iterate thru and update all datetime fields (which is a varchar type) check if it’s a date and if so, add 5hrs to account for utc adjustment or if it’s an invalid date just set it to null. Below is a template I came up with. Just wondering if there are some other ways to do this?
update Tables_1
set releasedate = CASE ISDATE(releasedate)
WHEN 1 THEN DATEADD(HH, 5, releasedate)
ELSE NULL
END,
returndate = CASE ISDATE(returndate)
WHEN 1 THEN DATEADD(HH, 5, returndate)
ELSE NULL
END
given: sql server 2008, i know the columns and tables already that have the datetime type stored as varchar.
bonus request to add another check. If it is a date and the time is not specified, set the time to 5:00 AM
If they all end with
dateyou could build it dynamically:Now if you want to do that for 12 tables, you could just do this in a loop, e.g.