I am trying to add hours to current time like
-- NOT A VALID STATEMENT -- SELECT GetDate(DATEADD (Day, 5, GETDATE())) How can I get hours ahead time in SQL Server?
16 Answers
DATEADD (datepart , number , date )
declare @num_hours int; set @num_hours = 5; select dateadd(HOUR, @num_hours, getdate()) as time_added, getdate() as curr_date 0Select JoiningDate ,Dateadd (day , 30 , JoiningDate) from Emp Select JoiningDate ,DateAdd (month , 10 , JoiningDate) from Emp Select JoiningDate ,DateAdd (year , 10 , JoiningDate ) from Emp Select DateAdd(Hour, 10 , JoiningDate ) from emp Select dateadd (hour , 10 , getdate()), getdate() Select dateadd (hour , 10 , joiningDate) from Emp Select DateAdd (Second , 120 , JoiningDate ) , JoiningDate From EMP 1The DATEADD() function adds or subtracts a specified time interval from a date.
DATEADD(datepart,number,date) datepart(interval) can be hour, second, day, year, quarter, week etc; number (increment int); date(expression smalldatetime)
For example if you want to add 30 days to current date you can use something like this
select dateadd(dd, 30, getdate()) To Substract 30 days from current date
select dateadd(dd, -30, getdate()) declare @hours int = 5; select dateadd(hour,@hours,getdate()) 1SELECT GETDATE() + (hours / 24.00000000000000000) Adding to GETDATE() defaults to additional days, but it will also convert down to hours/seconds/milliseconds using decimal.
If you are using mySql or similar SQL engines then you can use the DATEADD method to add hour, date, month, year to a date.
select dateadd(hour, 5, now()); If you are using postgreSQL you can use the interval option to add values to the date.
select now() + interval '1 hour';