SQL WHERE depending on day of week - sql

I have a requirement to check records up to differing dates, depending on which day of the week it is currently.
On a Friday I need for it to look at the entire next week, until Sunday after next. On any other day it should check the current week, up until the coming Sunday.
I have the below currently but it's not working due to syntax error. Is it possible to do a CASE WHEN inside a WHERE clause?
WHERE
T0.[Status] IN ('R','P')
AND
CASE
WHEN DATEPART(weekday,GETDATE()) = '5'
THEN T0.[DueDate] >= GETDATE() AND <= DATEADD(day, 15 - DATEPART(weekday, GetDate()), GetDate())
WHEN DATEPART(weekday, GETDATE()) != '5'
THEN T0.[DueDate] >= GETDATE() AND <= DATEADD(DAY ,8- DATEPART(weekday, GETDATE()), GETDATE())
END

It's much easier to create this logic with a series of logical or and and operators:
WHERE
T0.[Status] IN ('R','P') AND
((DATEPART(weekday,GETDATE()) = '5' AND
T0.[DueDate] >= GETDATE() AND
T0.[DueDate] <= DATEADD(day, 15 - DATEPART(weekday, GetDate()), GetDate())) OR
(DATEPART(weekday,GETDATE()) != '5' AND
T0.[DueDate] >= GETDATE() AND
T0.[DueDate] <= DATEADD(DAY ,8- DATEPART(weekday,GETDATE()),GETDATE())
)

Your syntax is wrong, you are using a condition evaluation in the THEN clause instead of an assignemnet
WHEN DATEPART(weekday,GETDATE()) = '5' THEN Your_column1 ELSE your_column2 END
......
or a inner case
CASE
WHEN DATEPART(weekday,GETDATE()) = '5' THEN
CASE WHEN T0.[DueDate] >= GETDATE()
AND <= DATEADD(day, 15 - DATEPART(weekday, GetDate()), GetDate())
THEN Your_column1 ELSE your_column2
END
END
......

Related

How to include Saturday and Sunday data in Sql query

I would like to pull data into my query for JUST the previous day. When I run the query on Monday I would like to have data for Saturday and Sunday as well.
The below part of the query works fine but it does not include Sat and Sun when ran on a Monday. Can anyone give me an idea of what to add?
where ([ImportDate] = DATEADD(DAY, CASE DATENAME(WEEKDAY, GETDATE())
WHEN 'Sunday' THEN -2
WHEN 'Monday' THEN -3
ELSE -1 END,
DATEDIFF(DAY, 0, GETDATE())))
You were close. But you want to make it a range of dates that would include the previous days you want up to the current date, by using >= < or a BETWEEN.
where [ImportDate] >= DATEADD(DAY, CASE DATENAME(WEEKDAY, GETDATE()) WHEN 'Sunday' THEN -2 WHEN 'Monday' THEN -3 ELSE -1 END, DATEDIFF(DAY, 0, GETDATE()))
and [ImportDate] < CONVERT(DATE,GETDATE())

Categorize data by time periods

I am looking for a SQL statement to select all rows from the table with time periods like Today, Yesterday..etc. The table holds one datetime column.
Here result set should automatically populate results with pretty date (Today, yesterday,.. Last month, Older)and number of records on the periods,
Today -- 10
Yesterday -- 35
If you are looking to define the time periods, then use case. You don't really define the exact definitions, but something like this:
select (case when col >= convert(date, getdate()) then 'Today'
when col >= dateadd(day, -1, convert(date, getdate())) then 'Yesterday'
when col >= dateadd(day, -6, convert(date, getdate())) then datename(weekday, col)
when col >= dateadd(day, -13, convert(date, getdate())) then 'Last Week'
when col >= dateadd(day, -20, convert(date, getdate())) then 'Two Weeks Ago'
when col >= dateadd(day, -27, convert(date, getdate())) then 'Thre Weeks Ago'
else 'Older'
end)
Here is a db<>fiddle.

SQL: Pull previous day's activity (include weekend on Mondays)

I'm trying to return data from the previous day, except for on Monday I want to return data from the previous 3 days. The below seems logical to me, though I'm getting the error of Incorrect syntax near '='.
Any idea what I'm missing?
*Dates stored in datetime hence the CONVERT functions.
WHERE
CASE WHEN DATEPART(DW,GETDATE()) IN ('1')
THEN (CONVERT(DATE,EV.EVENT_DATE) = CONVERT(DATE,DATEADD(D,-3,GETDATE())))
ELSE (CONVERT(DATE,EV.EVENT_DATE) = CONVERT(DATE,DATEADD(D,-1,GETDATE())))
END
SQL Server does not support boolean expressions like this.
You can express this without the case, which is generally preferable:
WHERE ( DATEPART(WEEKDAY, GETDATE()) = 1 AND CONVERT(DATE, EV.EVENT_DATE) = CONVERT(DATE, DATEADD(DAY, -3, GETDATE()))
) OR
( DATEPART(WEEKDAY, GETDATE()) <> 1 AND CONVERT(DATE, EV.EVENT_DATE) = CONVERT(DATE, DATEADD(DAY, -1, GETDATE()))
)
EDIT:
If you want the weekend dates, then use inequalities. Assuming event_dates are not in the future:
You'll notice that I spelled out the date parts. I find this a better practice than trying to remember/figure out what a particular abbreviation might mean.
WHERE ( DATEPART(WEEKDAY, GETDATE()) = 1 AND CONVERT(DATE, EV.EVENT_DATE) >= CONVERT(DATE, DATEADD(DAY, -3, GETDATE()))
) OR
( DATEPART(WEEKDAY, GETDATE()) <> 1 AND CONVERT(DATE, EV.EVENT_DATE) >= CONVERT(DATE, DATEADD(DAY, -1, GETDATE()))
)

Exclude weekends from my query

I have a query which is working fine.
The query basically retrieves data between 6-8 days old from current date. I'd like exclude the weekends when measuring the age of the data.
Example: If a record is registered Friday, then Monday morning it will show like it's 4 days old, but it's actually only 2 days old, because Satuday and Sunday shouldn't count.
I tried this, which does not seem to be working:
Select id, name, CreatedDate
from table
where
CreatedDate <= DATEADD(day, -6, GETDATE()) AND CreatedDate >= DATEADD(day, -8, GETDATE()) -- here I get data between 6-8 days old
AND ((DATEPART(dw, CreatedDate) + ##DATEFIRST) % 7) NOT IN (0, 1) -- Here im trying to exclude weekends
What am I doing wrong?
You could see calendar and try this query
// Logic is very simple.
// See calendar and try.
// If today is Monday, then Prev8workingdays will include
// 8 working days + 2 weekends = 12 days.
// Then result will be dateadd(day,-12, getdate()) = 12 days before today.
// Same logic for other days week
DECLARE #Prev8workingdays date = CASE
WHEN datepart(dw, getdate()) IN (2,3,4) THEN dateadd(day,-12, getdate())
WHEN datepart(dw, getdate()) IN (1) THEN dateadd(day,-11, getdate())
ELSE dateadd(day,-10, getdate())
END
DECLARE #Pre6WorkingDay date = CASE
WHEN datepart(dw, getdate()) IN (2) THEN dateadd(day,-10, getdate())
WHEN datepart(dw, getdate()) IN (1) THEN dateadd(day,-9, getdate())
ELSE dateadd(day,-8, getdate())
END
SELECT sd.* FROM
#SampleDate sd
WHERE sd.CreatedDate >= #Prev8workingdays AND sd.CreatedDate <= #Pre6WorkingDay
Reference link DATEADD
you can try:
WHERE DATEPART(dw, date_created) NOT IN (1, 7);
Try this:
Select id, name, CreatedDate
from table
where CreatedDate < GETDATE()-6 and CreatedDate > GETDATE()-8 and
((DATEPART(dw, CreatedDate) + ##DATEFIRST) % 7) NOT IN (0, 1)

How do I compare against the current week using SQL Server?

How do I compare an SQL Server date column against the current week?
For instance:
WHERE [Order].SubmittedDate = *THIS WEEK*
You could convert your date to a week number and compare this to the week number from the current date. Likewise, you'll need to compare the year as well, so that you don't get last year's weeks.
WHERE DATEPART(wk, [Order].SubmittedDate) = DATEPART(wk, GETDATE())
AND DATEPART(yy, [Order].SubmittedDate) = DATEPART(yy, GETDATE())
Assuming you are meaning always "this week" and there are no records with Submitted dates in the future, which I imagine could be the case you can do:
WHERE [Order].SubmittedDate >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
If dates do go into the future, the full restriction to this week is:
WHERE [Order].SubmittedDate >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND [Order].SubmittedDate < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
I'd strongly recommend using a clause based on a start and end date like this, as it will allow efficient index use so should perform better.
Try this:
WHERE [Order].SubmittedDate BETWEEN
DATEADD(d, - DATEPART(dw, GETDATE()) + 1, GETDATE()) AND
DATEADD(d, 7 - DATEPART(dw, GETDATE()) , GETDATE())
Maybe this can run faster, as doesn't needs to be evaluated everytime:
DECLARE #StartDate DATETIME,
#EndDate DATETIME
SELECT #StartDate = DATEADD(d, - DATEPART(dw, GETDATE()) + 1, GETDATE()),
#EndDate = DATEADD(d, 8 - DATEPART(dw, GETDATE()) , GETDATE())
-- // Strip time part, so week starts on Sunday 00:00
SELECT #StartDate = CAST(FLOOR(CAST(#StartDate AS FLOAT)) AS DATETIME),
#EndDate = CAST(FLOOR(CAST(#EndDate AS FLOAT)) AS DATETIME)
...
WHERE [Order].SubmittedDate >= #StartDate AND [Order].SubmittedDate < #EndDate