Solved

How to use WHERE with DATEPART in MSSQL

Posted on 2012-03-13
3
551 Views
Last Modified: 2012-03-13
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
0
Comment
Question by:stressfreewebs
3 Comments
 
LVL 15

Accepted Solution

by:
Anuj earned 500 total points
ID: 37714793
SELECT aId, aStatus
FROM Availability
WHERE DATEPART(weekday,aDate) IN (5,6)
0
 
LVL 18

Expert Comment

by:lludden
ID: 37715053
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.
0
 

Author Closing Comment

by:stressfreewebs
ID: 37715535
Thanks...no idea why what I had wasn't working, but as soon as I swapped to this tidier version, it worked a treat!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now