Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Only when I add the AND

Author  Topic 

mnielsen
Starting Member

17 Posts

Posted - 2009-07-26 : 10:14:13
I'm getting a syntax error for the below:

select DATEPART(hour, DateAdd(hh,-1, getdate())) AND DATEPART(hour, getdate())

Each portion works find by itself, but when I put the AND in, it gets:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AND'

Any ideas?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-26 : 10:17:13
why the AND there ? what are you trying to achieve ?

this ?

select DATEPART(hour, DateAdd(hh,-1, getdate())) , DATEPART(hour, getdate())



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mnielsen
Starting Member

17 Posts

Posted - 2009-07-26 : 10:47:19
Thanks for the reply khtan!

This is the statement in the simplest form. Since the whole thing is Ginormous. Basically, trying to get records for current hour and previous hour.

Would yours give me that?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-26 : 10:51:19
yup. The query will give previous hour and current hour


select current_hour = datepart(hour, getdate()),
previous_hour = datepart(hour, dateadd(hour, -1, getdate()))



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mnielsen
Starting Member

17 Posts

Posted - 2009-07-26 : 11:00:21
Works seperately, but for some reason when I add it into my WHERE, it complains.

WHERE DATEPART(hour, M.TimeStamp) =
datepart(hour, getdate()) OR
datepart(hour, dateadd(hour, -1, getdate()))


Msg 170, Level 15, State 1, Line 28
Line 28: Incorrect syntax near ')'.

Go to Top of Page

mnielsen
Starting Member

17 Posts

Posted - 2009-07-26 : 11:07:50
Tried by assigning to varibles and recieve a different error:

DECLARE @CurrHour int;
DECLARE @PrevHour int;

SET @CurrHour = datepart(hour, getdate());
SET @PrevHour = datepart(hour, dateadd(hour, -1, getdate()));

Select * FROM dbo.Database
WHERE DATEPART(hour, M.TimeStamp) = @CurrHour OR @PrevHour

Msg 170, Level 15, State 1, Line 34
Line 34: Incorrect syntax near '@PrevHour'.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-26 : 11:23:01
[code]
Select * FROM dbo.Database M
WHERE DATEPART(hour, M.TimeStamp) = @CurrHour OR DATEPART(hour, M.TimeStamp) = @PrevHour
[/code]

It you be better to do it this way, if your TimeStamp column contain both date and time.

[code]
SELECT *
FROM dbo.Database M
WHERE M.TimeStamp >= DATEADD(HOUR, DATEDIFF(HOUR, 0, GETDATE()) - 1, 0)
AND M.TimeStamp < DATEADD(HOUR, DATEDIFF(HOUR, 0, GETDATE()) + 1, 0)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mnielsen
Starting Member

17 Posts

Posted - 2009-07-26 : 11:34:29
That is TERRIFIC!!! WOrks like a gem :o) Thank you!!! Why didn't I come here sooner
Go to Top of Page
   

- Advertisement -