Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 727
sd_dracula Avatar asked Aug 31 '26 14:08

sd_dracula


2 Answers

You can use DATEADD

UPDATE YourTable
SET Column2 = DATEADD(YEAR, 1, Column1)
WHERE Column1 IS NOT NULL
like image 189
Darren Avatar answered Sep 03 '26 03:09

Darren


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

That's gone to be ok

like image 32
Maxim Zhukov Avatar answered Sep 03 '26 05:09

Maxim Zhukov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!