- My answer assumes that you want to select rows where the
seriesdate
value is in a week that is not the current week.
- Assuming that your
seriesdate
column is indexed then a SARGable (i.e. fast) way to implement this filter is to just exclude rows with dates after the start-of-the-current-week. Like so...
- Also...
- Note that
CURRENT_TIMESTAMP
(the ISO SQL equivalent of SQL Server's GETDATE()
) uses the database server's local timezone which might not be the timezone used by your data.
- If so, replace
DECLARE @now datetime2(7) = CURRENT_TIMESTAMP;
with a date/time-literal like DECLARE @now datetime2(7) = '2022-08-10 12:34:56';
- Note that most SQL Server setups will default to
DATEFIRST 7
(Sunday).
SET DATEFIRST 1; -- https://learn.microsoft.com/en-us/sql/t-sql/statements/set-datefirst-transact-sql
DECLARE @now datetime2(7) = CURRENT_TIMESTAMP; -- or '2022-08-10 12:34:56'
-- https://stackoverflow.com/questions/7168874/get-first-day-of-week-in-sql-server
DECLARE @startOfThisWeek date = DATEADD( day, 1 - DATEPART( weekeday, @now ), @now );
SELECT
q.*
FROM
[RRA] AS q
WHERE
q.[seriesdate] < @startOfThisWeek