Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use WHERE with DATEPART in MSSQL

Posted on 2012-03-13
3
Medium Priority
?
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
Anuj earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

609 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