Solved

SQL Last Weekday Statement

Posted on 2008-06-24
15
727 Views
Last Modified: 2010-07-27
Attached below is my sql query that is working fine.  I would like to add a WHERE clause to show

WHERE a.docdate = 'last weekday'

If today is Monday then I would want it to show all records where a.docdate='Friday'

Any ideas?
select a.sopnumbe, a.custname, a.cstponbr, a.docdate, a.user2ent, b.ponumber from 
(select * from sop10100 where docid = 'WARRANTY') A
left join
(select * from sop60100) B
on a.sopnumbe = b.sopnumbe
group by a.sopnumbe, a.custname, b.ponumber, a.docdate, a.cstponbr, a.user2ent

Open in new window

0
Comment
Question by:r270ba
  • 8
  • 4
15 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21856770
something like this?  You just want it where the date is Friday?

select a.sopnumbe, a.custname, a.cstponbr, a.docdate, a.user2ent, b.ponumber from
(select * from sop10100 where docid = 'WARRANTY') A
left join
(select * from sop60100) B
on a.sopnumbe = b.sopnumbe
where datename(dw, a.docdate) = 'Friday'
group by a.sopnumbe, a.custname, b.ponumber, a.docdate, a.cstponbr, a.user2ent
0
 

Author Comment

by:r270ba
ID: 21856867
Sorry...I was using Friday as an example.  What I am looking for is the previous weekday.  So, if today is Tuesday it would look for Monday's date.  If today is Monday it would look for Friday's date.  
0
 

Author Comment

by:r270ba
ID: 21856879
Today is 06/24/08 and I would want a.docdate='06/23/08'

Monday was 06/23/08 so I would want a.docdate='06/20/08' (Friday)
0
Technology Partners: 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!

 
LVL 60

Expert Comment

by:chapmandew
ID: 21856908
OK...try this:

select a.sopnumbe, a.custname, a.cstponbr, a.docdate, a.user2ent, b.ponumber from
(select * from sop10100 where docid = 'WARRANTY') A
left join
(select * from sop60100) B
on a.sopnumbe = b.sopnumbe
where convert(varchar(10), a.docdate , 101) = convert(varchar(10), case when datename(dw, getdate()) = 'Monday' THEN DATEADD(d, -3, getdate()) else dateadd(d, -1, getdate()), 101)
group by a.sopnumbe, a.custname, b.ponumber, a.docdate, a.cstponbr, a.user2ent
0
 

Author Comment

by:r270ba
ID: 21856972
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.

I have check the () and they appear to be correct.  Any ideas?
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 300 total points
ID: 21857028
sorry about that..try this:

select a.sopnumbe, a.custname, a.cstponbr, a.docdate, a.user2ent, b.ponumber from
(select * from sop10100 where docid = 'WARRANTY') A
left join
(select * from sop60100) B
on a.sopnumbe = b.sopnumbe
where convert(varchar(10), a.docdate , 101) = convert(varchar(10), (case when datename(dw, getdate()) = 'Monday' THEN DATEADD(d, -3, getdate()) else dateadd(d, -1, getdate())end), 101)
group by a.sopnumbe, a.custname, b.ponumber, a.docdate, a.cstponbr, a.user2ent
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 200 total points
ID: 21857145
select a.sopnumbe, a.custname, a.cstponbr, a.docdate, a.user2ent, b.ponumber from
(select * from sop10100 where docid = 'WARRANTY' and
    docdate >= dateadd(day, - case datename(weekday, getdate()) when 'monday' then 3 when 'sunday' then 2 else 1 end, convert(char(8), getdate(), 112)) and
    docdate < dateadd(day, - case datename(weekday, getdate()) when 'monday' then 2 when 'sunday' then 1 else 0 end, convert(char(8), getdate(), 112))
) A
left join
(select * from sop60100) B
on a.sopnumbe = b.sopnumbe
group by a.sopnumbe, a.custname, b.ponumber, a.docdate, a.cstponbr, a.user2ent
0
 

Author Comment

by:r270ba
ID: 21857907
Chapmandew...as always...thanks for the expert help!
0
 

Author Comment

by:r270ba
ID: 21857918
I am sorry but I did not realize that ScottPletcher had posted and I selected the last post as the answer thinking it was Chapmandew.  Can I please award the points correctly?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21857942
please do.  :)
0
 

Author Comment

by:r270ba
ID: 21858026
I need a mod to reset this post and let me re award the points.  Thanks and sorry!
0
 

Author Comment

by:r270ba
ID: 21858964
I have reqeuested for this to be re-opened.
0
 

Author Closing Comment

by:r270ba
ID: 31470191
Thanks for both of your help.  I gave Chapmandew 300 points because I used his solution but I feel like ScottPletcher actually had a more "defensive" solution by adding the if Sunday clause (even though I only thought about Monday).  I appreciate both of your help!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

735 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