Lakhae
Lakhae

Reputation: 1899

How to add hours to current date in SQL Server?

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?

Upvotes: 91

Views: 345140

Answers (6)

Codemaker2015
Codemaker2015

Reputation: 15716

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';

Upvotes: 7

Donavan R Lane
Donavan R Lane

Reputation: 65

SELECT GETDATE() + (hours / 24.00000000000000000)

Adding to GETDATE() defaults to additional days, but it will also convert down to hours/seconds/milliseconds using decimal.

Upvotes: 2

Arslan Bhatti
Arslan Bhatti

Reputation: 193

declare @hours int = 5;

select dateadd(hour,@hours,getdate())

Upvotes: 4

Nayas Subramanian
Nayas Subramanian

Reputation: 2379

The 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())

Upvotes: 3

user6851776
user6851776

Reputation: 99

Select 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

Upvotes: 9

gloomy.penguin
gloomy.penguin

Reputation: 5911

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  

Upvotes: 158

Related Questions