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 date in column

Author  Topic 

Matte
Starting Member

2 Posts

Posted - 2011-06-17 : 13:12:30
Hi I have a table which looks like this:
Index TimeStamp Counter_A Counter B Counter_C Counter D
71573 2011-08-08 22:50:00 0 0 76268 11508125
71574 2011-08-08 22:55:00 0 0 76268 11508309
71575 2011-08-08 23:00:00 0 0 76268 11508484
71576 2011-08-08 23:05:00 0 0 76268 11508661
71577 2011-08-08 23:15:00 0 0 76268 11509008

I managed to change the date on my computer by mistake a couple of days ago and did not realise this before today.
The TimeStamp column is therefore incorrect. It is 2 months ahead for some rows (a couple of thousand rows).

Is there an easy way to change the timeStamp column for all rows where date is 2011-08-dd xx:yy:zz to 2011-06-dd xx:yy:zz?

Br
Mat

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-17 : 13:42:47
Run the following query first. That will show you the current values and the new values they will be updated to. Then, if you are satisfied, run the second query. That will do the updating.

SELECT
[TimeStamp] AS CurrentValue,
DATEADD(mm,-2,[TimeStamp]) AS NewValue
FROM YOUR TABLE
WHERE
YEAR([TimeStamp]) = 2011
AND MONTH([TimeStamp]) = 8;

----
UPDATE YOURTABLE SET
[TimeStamp] = DATEADD(mm,-2,[TimeStamp])
WHERE
YEAR([TimeStamp]) = 2011
AND MONTH([TimeStamp]) = 8;
Be VERY careful about having the WHERE clause exactly as above. If not, the WHOLE table will get updated!
Go to Top of Page

Matte
Starting Member

2 Posts

Posted - 2011-06-17 : 16:34:15
Many thanks!

I managed to convert the syntax to mysql and it worked perfectly.

SELECT
TimeStamp AS CurrentValue,
DATE_ADD(TimeStamp,INTERVAL -2 YEAR_MONTH) AS NewValue
FROM msure.elmatare
WHERE
YEAR(TimeStamp) = 2011
AND MONTH(TimeStamp) = 8;


UPDATE msure.elmatare SET
TimeStamp = DATE_ADD(TimeStamp,INTERVAL -2 YEAR_MONTH)
WHERE
YEAR(TimeStamp) = 2011
AND MONTH(TimeStamp) = 8;


Go to Top of Page
   

- Advertisement -