SQL and DateTime calculations

Hi,
I'm trying to fill a variable with the current date + 2 hours.
the problem is that I want the minutes and the seconds to be 0.

What I succeeded to do is increasing the date with 2 hours (by DateAdd - see below)
But if the datetime is: 2008/01/13 18:30:19, it will change to 2008/01/13 20:30:19.
I want it to be changed to: 2008/01/13 20:00:00

This code I used so far:

declare @tempDate DateTime
declare @nextDate DateTime
set @tempDate = DATEADD(hour, 2, GetDate())

Thanks
giligatechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HuyBDCommented:
try this
declare @tempDate DateTime
declare @nextDate DateTime
set @tempDate = DATEADD(hour, 2, GetDate())
set @tempDate = DATEADD(minute, -minute(@tempDate), @tempDate)
set @tempDate = DATEADD(minute, -second(@tempDate), @tempDate)

Open in new window

0
giligatechAuthor Commented:
sorry, but it doesn't work.
Msg 195, Level 15, State 10, Line 4
'minute' is not a recognized built-in function name.
Msg 195, Level 15, State 10, Line 5
'second' is not a recognized built-in function name.

(MS SQL Server 2005)
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
use DATEPART(minute, @tempdate) instead of minute(@tempdate) (same for second)
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Chandan_GowdaCommented:

declare @tempDate DateTime
declare @nextDate DateTime
set @tempDate = DATEADD(hour, 2, GetDate())
set @tempDate = DATEADD(minute, -datepart(minute,(@tempDate)), @tempDate)
set @tempDate = DATEADD(second, -datepart(second,@tempDate), @tempDate)
SELECT @tempDate
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chandan_GowdaCommented:
---------Try this........
declare @tempDate DateTime
declare @nextDate DateTime
set @tempDate = DATEADD(hour, 2, GetDate())
set @tempDate = DATEADD(minute, -datepart(minute,(@tempDate)), @tempDate)
set @tempDate = DATEADD(second, -datepart(second,@tempDate), @tempDate)
set @tempDate = DATEADD(second, -datepart(second,@tempDate), @tempDate)
SELECT CONVERT(VARCHAR,@tempDate,20)
0
giligatechAuthor Commented:
thank you
0
Mark WillsTopic AdvisorCommented:
For what it is worth...

select dateadd(hh,datepart(hh,getdate())+2,convert(varchar,getdate(),106))
1
esmorenoCommented:
Mark Wills' solution is not only the most elegant - but it goes even farther by ensuring that the milliseconds is zero as well. Thanks Mark!
0
MohammedUCommented:
select  convert(varchar(13),dateadd(hh,2,getdate()),121)+':00:00.000'
OR
select  cast (convert(varchar(13),dateadd(hh,2,getdate()),121)+':00:00.000' as Datetime)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.