Get yesterdays date and if Monday get weekend range - vba

Is there a way to get yesterdays date. If the current date is monday I need three dates returned - Sunday, Saturday and Friday. Is there any way possible to accomplish this in a single query. I don't know VBA that well but if that is the only way to solve I am willing to get my hands dirty.
Select * from [Purchase Order] where MyDate = 'Yesterdays date(s)'

The WeekDay() function will tell you whether today's date, as returned by the Date() function, is Monday. Use that in an IIf() expression so that MyDate matches yesterday's date when today is not Monday, or the previous 3 dates when MyDate is Monday.
SELECT *
FROM [Purchase Order] AS p
WHERE
IIf(Weekday(Date()) = 2,
p.MyDate BETWEEN DateAdd('d',-3,Date())
AND DateAdd('d',-1,Date()),
p.MyDate=DateAdd('d',-1,Date())
);

Related

Recurring Date Calculator SQL

I'm looking to create a recurring date calculator that will correspond to EVENTS on a specific calendar date. Each event has its own rule.
For example, for EVENT A rule is "Occurs Tuesday monthly with the start date Tuesday 9-17-2019. I'm using a time dimension table with every date populated until 2025. Using these dates I was using in the WHERE clause
WHERE dayname = 'tuesday' and ( DATEDIFF(DAY, '2019-09-17', Calendar_Date) % 28 ) = 0 to get monthly tuesdays.
But I'm running into issues when there are two 5 Tuesdays in a month. 3-3-2020 and 3-31-2020. Need date populated to be 4-7-2020 instead of 3-31-2020.
Can anyone help with a solution?
With your query, you're pretty close. All you need to do is to then sub-select
SELECT MIN(CalendarDate) AS CalendarDate
FROM (your query goes here) AS d
GROUP BY YEAR(CalendarDate), MONTH(CalendarDate);

How to decipher complex DATEADD function from MS Access

I have inherited a query from an old MS Access DB and cannot for the life of me figure out what was trying to be done in this date parameter function. I normally only use SQL and this seems a bit different. Can any one assist in describing what this logic is doing?
use pdx_sap_user
go
select po_number,
po_issue_date
from vw_po_header
where po_issue_date > getDate() And PO_issue_date < DateAdd("d",-1,DateAdd("m",8,DateAdd("d",-(Day(getDate())-1),getDate())))
You can de-obfuscate it a lot by using DateSerial:
where
po_issue_date > getDate() And
po_issue_date < DateSerial(Year(getDate()), Month(getDate()) + 8, 0)
First: there is no getDate() function in Access. Probably it should be Date() which returns the current date.
Now starting from the inner expression:
Day(Date()) returns the current day as an integer 1-31.
So in DateAdd("d", -(Day(Date())-1), Date()) from the current date are subtracted as many days as needed to return the 1st of the current month.
Then:
DateAdd("m", 8, DateAdd("d", -(Day(Date())-1), Date()))
adds 8 months to the the 1st of the current month returning the 1st of the month of the date after 8 months.
Finally:
DateAdd("d", -1,...)
subtracts 1 day from the date returned by the previous expression, returning the last day of the previous month of that date.
So if you run today 13-Sep-2019 this code, the result will be:
30-Apr-2020
because this is the last day of the previous month after 8 months.
I think the following:
Take the current date
Substract the current day of month -1 to get the first day of current month
Add 8 month to this
Substract 1 day to get the last day of the previous month
So it calculates some deadline in approx 8 months.
But I wonder how a PO issue date can be in the future...

SQL Get interval last week (sunday to saturday)

I need a simple sql script where i can grab the rows with a date between sunday 2 weeks ago and saturday the previous week.
I need the query to return the elements no matter what day of this week I run the query.
Lets say I run the query today: Thursday 12. dec 2016. (12-08-2016)
I need to get this interval:
SELECT * FROM table WHERE date BETWEEN '11-27-2016' AND '12-03-2016'
DECLARE #StartInterval DATE,
#EndInterval DATE,
#Today = GETDATE()
SET #EndInterval = DATEADD(dd,-1,DATEADD(dd,-1*(DATEPART(dw,#Today)-1),#Today))
SET #StartInterval = DATEADD(dd,-6,#EndInterval)
SELECT *
FROM table
WHERE date BETWEEN #StartInterval AND #EndInterval
You can use:
select * from table where date between next_day(date, 'SUN')-21 AND
next_day(date, 'SAT')-14

create a view to have relative dates

I have a query which returns data between two dates. Typically those dates are Monday the previous week & Friday the previous week. I've been asked to make this query a view. I have instead made it a stored procedure where the user can enter the two dates. However I would like to know if it would be possible to create a view purely out of my own interest.
Eg. Today is Thursday 21st April. So the from date would be Monday 11th April and the to date would be Friday 15th April.
Is there anyway to setup my query so no matter what day it is this week it will select Monday & Fridays date? Then obviously next week it would automatically select the from date as 18th & the to date as 22nd?
To find Monday and Friday of last week (this is not sensitive to DATEFIRST):
CAST(DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE())-1,0) AS DATE) 'Monday Last Week'
CAST(DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE())-1,4) AS DATE) 'Friday Last Week'
This works because Date = 0 was a Monday:
SELECT DATENAME(WEEKDAY, 0) 'What Was Day Zero'
What Was Day Zero
------------------------------
Monday
So your view would be something like (untested):
CREATE VIEW SomeView AS
SELECT
SomeCols
FROM
SomeTable S
WHERE S.SomeDate BETWEEN
CAST(DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE())-1,0) AS DATE)
AND
CAST(DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE())-1,4) AS DATE);
I liked #Les H's solution. Thinking this might be needed for a column whose type is not date, but something like datetime, datetime2 I think something like this might be better:
CREATE VIEW SomeView AS
SELECT
SomeCols
FROM
SomeTable S
WHERE
S.SomeDate >= DATEADD(WEEK,DATEDIFF(WEEK,0+(7*1),GETDATE()),0)
AND
S.SomeDate < DATEADD(WEEK,DATEDIFF(WEEK,0+(7*1),GETDATE()),5)
Here I propose a defensive coding against selecting date\time values with a range (between wouldn't work) and I just slightly changed the expression to make it a little more obvious what we are doing and one could simply change the *1 part to mean N weeks before (less tricky I guess).

SQL Query Return All Date which is weekend from a table

I have a table which has a field called Date , contains a list of date, and now I want to return a table contains a list of Date which is weekend with SQL Query.
I am using Microsoft Access and so far I have tried DATEPART and WEEKDAY, but they can only check if a specified date is weekend or not, not a list of Date.
How can I do that? Thank You
This seems to explain what you need...
http://www.techonthenet.com/access/functions/date/datepart.php
DATEPART("w" will give the weekday from 1 to 7.
vbMonday will ensure that Monday is weekday 1.
=>
Saturdays are weekday 6 and Sundays are weekday 7.
WHERE
DATEPART("w", yourField, vbMonday) >= 6