sd_dracula sd_dracula - 7 months ago 28
SQL Question

SQL update datetime entries

I have a table with a list of dates of datetime format in two columns.

Now what I would like to do is pull each non null date from

Column1
, add one year to each value and then store it into
Column2
.

So below, after the update,
Column2
(the one with the nulls) will show:

2014-07-09 00:00:00.000
2013-07-30 00:00:00.000
2013-10-19 00:00:00.000
2013-10-19 00:00:00.000


enter image description here

How does the syntax go? Do I need to do a select followed by an update?

Answer
UPDATE tbl
SET Column2 = DATEADD(year, 1, Column1)
WHERE Column2 IS NULL AND Column1 IS NOT NULL

That's gone to be ok