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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Upcoming Weekend Only
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mattie
Starting Member

13 Posts

Posted - 12/14/2012 :  12:57:38  Show Profile  Reply with Quote
I have a field called Sched_Date that I use in a Case Statement: Case When datepart (weekday, SCHED_DATE) = 1 Then 'Sunday' When datepart (weekday, SCHED_DATE) = 7 Then 'Saturday' End as Weekend

I want to be able to only include the sched_Date for the upcoming Weekend in the Where Clause, and include both Saturday and Sunday. I would like it to run automatically, so I don't have to rewrite the dates each time.

Is this possible? Thx

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/14/2012 :  13:16:26  Show Profile  Reply with Quote
You can do the following. Check the boundary conditions though - like, if today is a Sunday, do you want to consider the following Saturday as a weekend or not etc.
SELECT CASE 
            WHEN CASE 
                      WHEN DATEPART(weekday, c) = 1 THEN 'Sunday'
                      WHEN DATEPART(weekday, SCHED_DATE) = 7 THEN 'Saturday'
                 END 
                 AND DATEDIFF(dd, GETDATE(), SCHED_DATE) > 6 THEN 1
            ELSE 0
       END AS Weekend
A bit shorter code that is also language independent is as follows:
SELECT CASE 
            WHEN DATEDIFF(dd, 0, SCHED_DATE)%7 > 4 
                 AND DATEDIFF(dd, GETDATE(), SCHED_DATE) > 6 THEN 1
            ELSE 0
       END AS Weekend
Go to Top of Page

mattie
Starting Member

13 Posts

Posted - 12/14/2012 :  13:39:42  Show Profile  Reply with Quote
Yes, when the query is run on Saturday or Sunday, I would like it to query the CURRENT Weekend. When it's any other day, the following weekend. Is there a way to do this in the Where Clause without the CASE? I keep getting syntax errors when I try this.
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/14/2012 :  13:55:53  Show Profile  Reply with Quote
I am not seeing any syntax error in the code that I posted. Can you post the code that you are seeing the error in and also the error message?
Go to Top of Page

shilpash
Posting Yak Master

101 Posts

Posted - 12/14/2012 :  13:56:53  Show Profile  Reply with Quote
SELECT MAX(SCHED_DATE)
,DATEPART(weekday,SCHED_DATE)
FROM TABLENAME
WHERE DATEPART(weekday,SCHED_DATE) = 7
OR DATEPART(weekday,SCHED_DATE) = 1
GROUP BY DATEPART(weekday,SCHED_DATE)
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/14/2012 :  14:07:30  Show Profile  Reply with Quote
quote:
Originally posted by shilpash

SELECT MAX(SCHED_DATE)
,DATEPART(weekday,SCHED_DATE)
FROM TABLENAME
WHERE DATEPART(weekday,SCHED_DATE) = 7
OR DATEPART(weekday,SCHED_DATE) = 1
GROUP BY DATEPART(weekday,SCHED_DATE)

I am not seeing any syntax error in this code. Can you post the error message you are getting?
Go to Top of Page

mattie
Starting Member

13 Posts

Posted - 12/14/2012 :  14:43:33  Show Profile  Reply with Quote
The last post seemed to work, but it shows all the weekends in December, instead of just the following weekend, i.e. 12/15 and 12/17. Thx!
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/14/2012 :  14:47:36  Show Profile  Reply with Quote
quote:
Originally posted by mattie

The last post seemed to work, but it shows all the weekends in December, instead of just the following weekend, i.e. 12/15 and 12/17. Thx!

True, it would do that - but you did not include the additional condition in the where clause that I had suggested. Add that as well.
Go to Top of Page

mattie
Starting Member

13 Posts

Posted - 12/14/2012 :  15:01:48  Show Profile  Reply with Quote
Can you tell me what I'm missing in the following? Thx for your help.

Select MAX(Sched_Date) , datepart (weekday, SCHED_DATE) as Day_Week
From Table

where
(DATEPART(weekday,SCHED_DATE) = 7
OR DATEPART(weekday,SCHED_DATE) = 1)

GROUP BY DATEPART(weekday,SCHED_DATE)
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/14/2012 :  15:07:48  Show Profile  Reply with Quote
SELECT MAX(Sched_Date),
       DATEPART(weekday, SCHED_DATE) AS Day_Week
FROM   tbl
WHERE  (
           ( DATEPART(weekday, SCHED_DATE) = 7
		OR DATEPART(weekday, SCHED_DATE) = 1
           ) AND DATEDIFF(dd, GETDATE(), SCHED_DATE) > 6
       )
GROUP BY
       DATEPART(weekday, SCHED_DATE)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/14/2012 :  15:14:01  Show Profile  Reply with Quote
for just getting following weekend this is enough

SELECT MAX(SCHED_DATE)
,DATEPART(weekday,SCHED_DATE)
FROM TABLENAME
WHERE SCHED_DATE >=DATEADD(day,(DATEDIFF(day,0,GETDATE())/7) * 7,5)
AND SCHED_DATE < DATEADD(day,(DATEDIFF(day,0,GETDATE())/7) * 7,7)
GROUP BY DATEPART(weekday,SCHED_DATE)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/14/2012 :  15:14:51  Show Profile  Reply with Quote
also make sure you read this

http://visakhm.blogspot.in/2012/08/creating-server-independent-day.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mattie
Starting Member

13 Posts

Posted - 12/14/2012 :  16:35:04  Show Profile  Reply with Quote
This worked great. Reading article also now. Thx to both of you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/15/2012 :  12:18:58  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.11 seconds. Powered By: Snitz Forums 2000