Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Exclude Weekends in SQL Query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

19 Posts

Posted - 08/26/2014 :  05:56:45  Show Profile  Reply with Quote
I have a sql query which checks for records in 2 days time.
However if I run this on a Thursday or Friday, it will display weekend date, which I want to exclude.

Can someone help so that it excludes Sat/Sun from query so if run on a friday it displays Tuesdays results?

Many thanks in advance.

Query is:

SELECT DISTINCT Orde_OrderQuoteId, orde_reference, orde_description, orde_status, orde_grossamt, orde_grossamt_CID FROM vlistorders WHERE (DATEPART(DY, orde_expiredelivery) = DATEPART(DY, DATEADD(DD, 2, GETDATE())) AND DATEPART(YY, orde_expiredelivery) = DATEPART(YY, DATEADD(DD, 1, GETDATE()))) AND Orde_OrderQuoteId IS NOT NULL ORDER BY Orde_OrderQuoteId

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 08/26/2014 :  10:18:19  Show Profile  Reply with Quote
Change your WHERE clause for the date arithmetic to this:
	orde_expiredelivery = DATEADD(dd,CASE WHEN DATEDIFF(dd,'19000101',GETDATE())%7 IN (3,4) THEN 4 ELSE 2 END,CAST(GETDATE() AS DATE))
Go to Top of Page

Starting Member

19 Posts

Posted - 08/26/2014 :  10:46:30  Show Profile  Reply with Quote
Perfect thanks James, works a treat
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.2 seconds. Powered By: Snitz Forums 2000