I have a query that runs once a day that I would like to share however I need to remove the part where the other users in my team will have to edit it. Essentially, it's run Monday thru Friday. I want, if today is Monday, give me the last 3 days worth of data. Any other day, just give me yesterday's data.
So far this is what I have:
Update: They are all strings, so now I get the following error.
"Incorrect syntax near the keyword 'BETWEEN'."
DECLARE @daychecker varchar(max) = FORMAT(GETDATE(), 'dddd')
DECLARE @daterange0 varchar(max)
DECLARE @daterange1 varchar(max) = FORMAT(GETDATE()-3, 'yyyy-MM-dd')
DECLARE @daterange2 varchar(max) = FORMAT(GETDATE()-1, 'yyyy-MM-dd')
IF @daychecker = 'Wednesday'
BEGIN
SET @daterange0 = BETWEEN @daterange1 AND @daterange2
END
ELSE
BEGIN
SET @daterange0 = FORMAT(GETDATE()-1, 'yyyy-MM-dd')
END
SELECT @daterange0;
The result for today as an example should return yesterday's date. But that doesn't work. I will consider all options including hardcoding some sort of master start date that we can count from like maybe the start of the year or something.
SET @daterange0 = BETWEEN @daterange1 AND @daterange2is not valid syntax