October 31, 2008 at 7:51 am
I have a very simple query that supposes to return a number
of records within the last 5 days, and for some strange reason,
I could not get it. It keeps giving me the error below.
Msg 4145, Level 15, State 1, Line 9
An expression of non-boolean type specified in a context where a condition is expected, near 5.
select id, name, changedate
from table1
where changedate - 5
-- changedate is datetime data type
I also tried to convert the date to mm/dd/yyyy format before
subtract the days, and it still give the same error.
What is wrong with my query? would appreciate any input.
October 31, 2008 at 7:57 am
Syntax issue.
Use this instead:
select id, name, changedate
from table1
where changedate > GETDATE()-5
October 31, 2008 at 7:58 am
The where clause must be a condition that returns true or false. Your is not. changedate-5 is simply a value, it's not a condition. It will return a date 5 days less than changedate
Base on what you said, I'm guessing you want is something a bit more like this.
DECLARE @Today DATETIME, @FiveDaysAgo DATETIME
SET @Today = (dd, datediff(dd,0, getdate()),0) -- today at midnight
SET @FiveDaysAgo = DATEADD(dd,-5,@Today) -- 5 days ago at midnight
select id, name, changedate
from table1
where changedate >= @FiveDaysAgo
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 31, 2008 at 7:59 am
The problem is the '-' your where clause
where changedate - 5
this does not make sense, it should have a '=' , ' '
eg
where changedate = 5
October 31, 2008 at 7:59 am
you need to specify what you are comparing changedate - 5 with. at the moment, the query is saying that changedate - 5 should give either a true or false answer.
you would need something like:
select id, name, changedate
from table1
where changedate >= dateadd(day, -5, getdate())
October 31, 2008 at 8:01 am
I see, thanks so much Seth and Gail for your quick responses.
really appreciated.
October 31, 2008 at 8:02 am
Garadin (10/31/2008)
Syntax issue.Use this instead:
select id, name, changedate
from table1
where changedate > GETDATE()-5
Beware of the time portion of dates. That, if run now, would miss any entries with a time before 4pm (local time for me). That may be what's wanted, it probably isn't though.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 31, 2008 at 8:07 am
Also see if you want this
select id, name, changedate
from table1
where changedate > DATEADD(DAY,DATEDIFF(DAY,0,GETDATE()),-5)
Failing to plan is Planning to fail
October 31, 2008 at 8:08 am
GilaMonster (10/31/2008)
Garadin (10/31/2008)
Syntax issue.Use this instead:
select id, name, changedate
from table1
where changedate > GETDATE()-5
Beware of the time portion of dates. That, if run now, would miss any entries with a time before 4pm (local time for me). That may be what's wanted, it probably isn't though.
Indeed. But I wouldn't have beaten you if I had typed all that! :hehe: (Kidding). It's a good reminder, I'm used to using it for larger, more general purposes such as GETDATE()-180, where the time isn't an important factor.
October 31, 2008 at 8:12 am
thank you all for your inputs, really helpful and appreciated a lot. i got it to work now by your suggestions, so you made my day...have a happy halloween.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply