| 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 1Incorrect 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] |
 |
|
|
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? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-26 : 10:51:19
|
yup. The query will give previous hour and current hourselect current_hour = datepart(hour, getdate()), previous_hour = datepart(hour, dateadd(hour, -1, getdate())) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
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 28Line 28: Incorrect syntax near ')'. |
 |
|
|
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.DatabaseWHERE DATEPART(hour, M.TimeStamp) = @CurrHour OR @PrevHourMsg 170, Level 15, State 1, Line 34Line 34: Incorrect syntax near '@PrevHour'. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-26 : 11:23:01
|
[code]Select * FROM dbo.Database MWHERE 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 MWHERE 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] |
 |
|
|
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 |
 |
|
|
|
|
|