Avatar of stressfreewebs
stressfreewebs
 asked on

How to use WHERE with DATEPART in MSSQL

Hi There,

I'd like to pull out records where the day of the week is Friday or Saturday. So I have the following query:

SELECT aId, aStatus
FROM Availability
WHERE DATEPART(weekday,aDate) = 5 OR DATEPART(weekday,aDate) = 6

Open in new window


I get an error message thus:

Microsoft OLE DB Provider for SQL Server error '80040e14'

An expression of non-boolean type specified in a context where a condition is expected

Is it possible to use Datepart in this way, and if so, how can I fix this query? If not, what is the best way to achieve the same result?

Thank you,

Dan
Microsoft SQL ServerASP

Avatar of undefined
Last Comment
stressfreewebs

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Anuj

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lludden

The query works fine for me.  You can try putting parenthesis around the conditions, or using IN as suggested above.

Keep in mind that doing a query like that, with calculations on a database field, will always result in a table scan, so performance may be pretty poor if the table is large.  Use a calendar table or an indexed computed field if you need to improve performance.
stressfreewebs

ASKER
Thanks...no idea why what I had wasn't working, but as soon as I swapped to this tidier version, it worked a treat!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy