SQL Query

The following query:  select dateadd (hh, DATEDIFF(hh,0,getdate()), 0)
 gives me 2015-10-13 13:00:00:000, or the current date and hour.

How do I get 2015-10-13 12:00:00:000 and 2015-10-13 11:00:00:000?
LVL 1
isamesAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
For starters, explain what this is, especially the begindate parameter of 0
SELECT DATEDIFF( hh,0,getdate())

Open in new window

If your question is 'how can I subtract an hour or two from any datetime value?', then use DATEADD.
Declare @dt datetime = GETDATE()

SELECT 
   @dt as now, 
   DATEADD(hh, -1, @dt) as one_hour_ago, 
   DATEADD(hh, -2, @dt) as two_hours_ago

Open in new window

0
Mike EghtebasDatabase and Application DeveloperCommented:
select dateadd(hh,12, convert (datetime, convert(date,getdate())))
2015-10-13 12:00:00.000
0
isamesAuthor Commented:
Sorry for not being clearer on my goal.

So what I'm trying to do is get the current date and hour, with no min and seconds. Also get the top of the previous 2 hours. So no matter when I run the query I would get the following, depending on what time it is:

current date with hour
current date with hour - 1
current date with hour - 2

no min or seconds etc.
0
Ultimate Tool Kit for Technology Solution Provider

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 now.

Scott PletcherSenior DBACommented:
select dateadd (hh, DATEDIFF(hh,0,getdate()) - 2, 0)
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Ok.  I'm guessing that there's a more elegant way to pull this off, but the  below code works:
Declare @dt datetime = GETDATE(), @dt_rounded_by_hr datetime, @hr int

-- Grab the hour component
SELECT @hr = DATEPART(hour, @dt) 

-- Round by hour:  Parse a string for the day + hour component, format as date
SELECT @dt_rounded_by_hr = 
   CAST(LEFT(convert(varchar, @dt ,120), 11)  + 
   RIGHT('0' + CAST(@hr as varchar(2)),2) + 
   ':00:00' as datetime) 

SELECT 
   @dt_rounded_by_hr as now, 
   DATEADD(hh, -1, @dt_rounded_by_hr) as one_hour_ago, 
   DATEADD(hh, -2, @dt_rounded_by_hr) as two_hours_ago

Open in new window

0
PortletPaulfreelancerCommented:
select dateadd (hh,       DATEDIFF(hh,0,    getdate()   )         , 0)

this middle part of your existing snippet (in italics) calculates the number of hours since 1900-01-01 00:00:00 until GETDATE() - the current date and time..

that result is an integer number of hours

in the outer portion of the snippet you add that result number of hours to 1900-01-01, and hence you get the effect of "rounding down" to the hour.


so. If you want one more hour less, or two hours less, adjust the result of the middle bit


select dateadd (hh,       DATEDIFF(hh,0,    getdate()   )         , 0)
select dateadd (hh,       DATEDIFF(hh,0,    getdate()   )  -1       , 0) -- one hour less
select dateadd (hh,       DATEDIFF(hh,0,    getdate()   )  -2       , 0) -- two hours less

========= + edit
select dateadd (hh,       DATEDIFF(hh,0,    getdate()   )         , 0)
                                                                  ^                                   ^
Those up arrows are pointing to 2 instances of zero
BOTH of those are "the zero date", which in MS SQL = 1900-01-01

in other words, the counting of days or hours starts at zero and that reference point expressed as a date/time is 1900-01-01 00:00:00.00000000
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
Umair QamarCommented:
select dateadd(hh,12, convert (datetime, convert(date,getdate()))) --[For Current Day and 12 hour Format time]
select dateadd(hh,-12, convert (datetime, convert(date,getdate()))) -- [For Previous Day and 12 hour Format time]
select dateadd(hh, +5, convert(datetime,GETUTCDATE())) -- [For Current Day and UTCTime as +5 is the asia region time]
select dateadd(hh, -9, convert(datetime,GETUTCDATE())) -- [For Current Day and UTCTime as -9 is the US region time]
select dateadd(DD, +5, convert(datetime,GETUTCDATE())) -- [For 9 Days future time and UTCTime as and 5 hours back in time]
select DATEADD(dd,0 , GETDATE()) -- [It gives you the correct date and time as it's the time on which is your computer showing in clock]
select dateadd (hh, DATEDIFF(hh,0,getdate()) - 2, 0) -- [Shows date and time (two hours back) and also shows minutes and seconds 00]
select dateadd (HH, DATEDIFF(hh,0,getdate()) -1, 0) -- [Shows date and time (one hour back) and also shows minutes and seconds 00]
select dateadd (DD, DATEDIFF(DD,0,getdate()) , 0) -- [only for date with 00 time]
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 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.