How to add an hour in timestamp in sql server (without Declare)

chrysa22 picture chrysa22 · Mar 1, 2019 · Viewed 8.2k times · Source

I want to add an hour in the parameter TimeStamp, but not with declare parameter i.e

DECLARE @datetime2 datetime2 = '2019-03-01T09:25:21.1+01:00'
                SELECT DATEADD(hour,1,@datetime)

I have a column name TimeStamp in a table and i want to add in all data plus 1 hour.

The column

TimeStamp 
2019-03-01T09:25:20.1+01:00
2019-03-01T09:25:21.1+01:00
2019-03-01T09:25:19.1+01:00

I try something like this

SELECT DATEADD(hour,1, TimeStamp), but i have an error 

Conversion failed when converting date and/or time from character string.

Any possible answers ?? Thanks

Answer

Bikram Limbu picture Bikram Limbu · Mar 1, 2019

SELECT DATEADD(hour,1, TimeStamp) is correct

However, The format in TimeStamp is wrong,

So, cast it to DateTime2 First

CAST(TimeStamp as DateTime2)

OR

CAST('2019-03-01T09:25:20.1+01:00' as DateTime2)

So,

SELECT DATEADD(hour, 1, CAST(TimeStamp as DateTime2))