Asad
Asad

Reputation: 21928

update DateTime value in Table using SQL

How can we update datetime value in a column using sql

say, if we want to update any of datetime values by adding an hour or 5 minutes etc.

UPDATE TableLastCalls
SET [NEXT_UPDATE_TIME] =  ?? // want to add an hour

Upvotes: 1

Views: 15924

Answers (5)

Phil Ross
Phil Ross

Reputation: 26120

You can use the DATEADD function:

UPDATE TableLastCalls
SET [NEXT_UPDATE_TIME] = DATEADD(hour, 1, [NEXT_UPDATE_TIME])

This will add 1 hour. Use DATEADD(minute, 5, [NEXT_UPDATE_TIME]) instead to add 5 minutes.

Upvotes: 14

Roatin Marth
Roatin Marth

Reputation: 24105

Use dateadd:

update TableLastCalls
set NEXT_UPDATE_TIME = dateadd(hh, 1, NEXT_UPDATE_TIME)

Upvotes: 2

marc_s
marc_s

Reputation: 755321

UPDATE TableLastCalls
SET [NEXT_UPDATE_TIME] = DATEADD(MINUTE, 5, NEXT_UPDATE_TIME)

UPDATE TableLastCalls
SET [NEXT_UPDATE_TIME] = DATEADD(HOUR, 2, NEXT_UPDATE_TIME)

and so on - lots of options with DATEADD to add specific amounts of time to your date.

See the MSDN docs on DATEADD for all the details.

Upvotes: 5

AdaTheDev
AdaTheDev

Reputation: 147344

UPDATE TableLastCalls
SET [NEXT_UPDATE_TIME] = DATEADD(hh, 1, [NEXT_UPDATE_TIME])
WHERE...

Upvotes: 3

womp
womp

Reputation: 116987

Use the DATEADD function.

UPDATE TableLastCalls
SET [NEXT_UPDATE_TIME] =  DATEADD(hour, 1, [NEXT_UPDATE_TIME])

Upvotes: 7

Related Questions