Select Query based on day of the week

bluelabel

Board Regular
Joined
Nov 27, 2008
Messages
76
I have a query that every day pulls yesterday's data from an sql database and drops it into Access.

SQL:
SELECT Date, InvoiceNo, Customer


FROM SalesDatabase


WHERE Date=Yesterdaysdate

What I need it to do though is when today's date is a Monday, I need it to gather the data for Friday, Saturday and Sunday, otherwise run the standard code.

So on a monday we have a special code that runs and picks up the last three days worth of data, every other day we want it to just pick up yesterdays data.

I hope that makes sense.

Thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
First, rename your date field...

Then try testing for the current day of week and use that to determine what to subtract.

SQL:
WHERE [dtmDateField] = DATE()-IIF(WEEKDAY(DATE(),2)=1,3,1)

 
Upvote 0
Hi @JonXL,

Thank you and that has worked.

The extension of this is when the day is a Monday I need it to pick up a date range of Friday-Sunday. In the formula given it picks up the Friday only.

How do I get it to pick up the date range of say 26/06 - 28/06 inclusive please and thank you.
 
Upvote 0
Change = to >= and add AND [dtmDateField] < Date(). Fully, like this:

SQL:
WHERE [dtmDateField] >= DATE()-IIF(WEEKDAY(DATE(),2)=1,3,1) AND [dtmDateField] < Date()
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top