How to update a date column from column with weeks - sql-server

I need to set the date to Monday of the week set in the week column
I created a query where there are differences:
select PROD_NR, BEWERK_DAT, BEWERK_WK, DATEPART(wk , BEWERK_DAT) as WeekIndex from Table1 where status = 3 and RIGHT(bewerk_wk,2) <> DATEPART(wk , BEWERK_DAT) order by id desc
Part of the output is like this:
PROD_NR BEWERK_DAT BEWERK_WK WeekIndex
P34619 2020-03-02 2020-09 10
P34619 2020-03-02 2020-09 10
P34619 2020-03-02 2020-09 10
P34619 2020-02-28 2020-08 9
P34619 2020-02-27 2020-08 9
I need to update the BEWERK_DAT column according to the week in BEWERK_WK
Output should be like:
PROD_NR BEWERK_DAT BEWERK_WK WeekIndex
P34619 2020-02-24 2020-09 9
P34619 2020-02-24 2020-09 9
P34619 2020-02-24 2020-09 9
P34619 2020-02-17 2020-08 8
P34619 2020-02-17 2020-08 8
Something like:
UPDATE Table1 SET bewerk_dat = "monday of the week part from BEWERK_WK" where there are differences.

The following may give you what you are looking for. I have provided a SELECT as well as the UPDATE.
The SET DATEFIRST is setting the first of the week to Monday
SET DATEFIRST 1
---
SELECT PROD_NR, BEWERK_DAT, BEWERK_WK , WeekIndex, CAST(SUBSTRING(BEWERK_WK,1,4) AS int) NewYear, CAST(SUBSTRING(BEWERK_WK,6,2) as int) NewWeek
,dateadd (week, CAST(SUBSTRING(BEWERK_WK,6,2) as int), dateadd (year, CAST(SUBSTRING(BEWERK_WK,1,4) AS int)-1900, 0)) - 4 -
datepart(dw, dateadd (week, CAST(SUBSTRING(BEWERK_WK,6,2) as int), dateadd (year, CAST(SUBSTRING(BEWERK_WK,1,4) AS int)-1900, 0)) - 4) + 1
FROM Table1
WHERE CAST(SUBSTRING(BEWERK_WK,6,2) as int) <> WeekIndex
---
UPDATE Table1 SET BEWERK_DAT = dateadd (week, CAST(SUBSTRING(BEWERK_WK,6,2) as int), dateadd (year, CAST(SUBSTRING(BEWERK_WK,1,4) AS int)-1900, 0)) - 4 -
datepart(dw, dateadd (week, CAST(SUBSTRING(BEWERK_WK,6,2) as int), dateadd (year, CAST(SUBSTRING(BEWERK_WK,1,4) AS int)-1900, 0)) - 4) + 1
WHERE CAST(SUBSTRING(BEWERK_WK,6,2) as int) <> WeekIndex

I found a way myself after a few hours more of searching.
set datefirst 1
go
update Table1 set BEWERK_DAT = CAST(DATEADD(WEEK, CONVERT(int, RIGHT(bewerk_wk,2)) - 1,DATEADD(dd, 1 - DATEPART(dw, '1/1/' + CONVERT(VARCHAR(4),CONVERT(int, LEFT(bewerk_wk,4)))), '1/1/' + CONVERT(VARCHAR(4),CONVERT(int, LEFT(bewerk_wk,4))))) as date)
where status = 3 and RIGHT(bewerk_wk,2) <> DATEPART(wk , BEWERK_DAT)
and before I could do this I had some fields in BEWERK_WK with a 9999-99 value which I had to reset first
update Table1 set BEWERK_WK = CONCAT(CONVERT(varchar, DATEPART(year, BEWERK_DAT)), '-', CONVERT(varchar, DATEPART(wk, BEWERK_DAT))) where bewerk_wk like '9%'

Related

SQL Server: Calculate Four Weeks From a Month

I need a simple solution to get 4 weeks for a month based on current date (each week starting from Monday - Friday).
For each week I need to update a table that already has current date and place a counter from Week 1 - 4 and continue to the following month starting from Week 6 - 8. and start from the beginning after week 8.
The query below is returning week number but for 7 days:
can I use something similar just for 5 days?
DECLARE #MyDate DATETIME = '2020-08-03'
--This assumes the weeks starts from Monday - Sunday
DECLARE #WeekNumber INTEGER = (DATEPART(DAY, DATEDIFF(DAY, 0, #MyDate)/7 * 7)/7 +1)
SELECT #WeekNumber
The previous answer was not useful so I got rid of it. This should do what you're looking for
declare #date datetime= '2020-08-03';
select dateadd(d, -4, dt.dt) start_dt,
dt.dt end_dt,
row_number() over (order by v.n) n
from
(select datefromparts(year(#date),month(#date),1) first_dt) fd
cross apply
(select datediff(week, 0, fd.first_dt) wk_diff) wd
cross apply
(values (1),(2),(3),(4),(5),(6)) v(n)
cross apply
(select dateadd(d, -((datepart(weekday, fd.first_dt) + 1 + ##datefirst) % 7), fd.first_dt) calc_dt) calc_dt
cross apply
(select dateadd(d, (v.n-1)*7, calc_dt) dt) dt
where
dt.dt>=fd.first_dt;
Results
start_dt end_dt n
2020-08-03 2020-08-07 1
2020-08-10 2020-08-14 2
2020-08-17 2020-08-21 3
2020-08-24 2020-08-28 4
2020-08-31 2020-09-04 5

SQL - Group By Week to begin on a specific weekday without involving two transactions?

I am writing a query that returns the sum of rows for the last 10 weeks FRI-THURS.
It uses a group by to show the sum of each week:
WITH Vars (Friday) -- get current week Fridays Date
AS (
SELECT CAST(DATEADD(DAY,(13 - (##DATEFIRST + DATEPART(WEEKDAY,GETDATE())))%7,GETDATE()) AS DATE) As 'Friday'
)
SELECT datepart(week, DateField) AS WeekNum, COUNT(*) AS Counts
FROM Table
WHERE DateField >= DATEADD(week,-9, (SELECT Friday from Vars))
GROUP BY datepart(week, DateField)
ORDER BY WeekNum DESC
The problem is every week starts on Monday so the Group By doesn't group the dates on how I want it. I want a week to be defined as FRI-THURS.
One workaround to this is to use DATEFIRST. e.g:
SET DATEFIRST = 5; --set beginning of each week to Friday
WITH Vars (Friday) -- get current week Fridays Date
... rest of query
However due to limitations on the interface I am writing this query I cannot have two separate statements run. It needs to be one query with no semicolons.
How can I achieve this?
This should do it. First pre-compute once the StartingFriday of 9 weeks ago, rather than doing that for each row. Then compute the dfYear and dfWeek giving them alias-es, where their DateField is after the starting friday. Lastly, Count/GroupBy/OrderBy.
Declare #StartingFriday as date =
DATEADD(week,-9, (DATEADD(day, - ((Datepart(WEEKDAY,GETDATE()) +1) % 7) , GETDATE())) ) ;
SELECT dfYear, dfWeek, COUNT(*) AS Counts
FROM
(Select -- compute these here, and use alias in Select, GroupBy, OrderBy
(Datepart(Year,(DATEADD(day, - ((Datepart(WEEKDAY,DateField) +1) % 7) , DateField)) ) )as dfYear
,(Datepart(Week,(DATEADD(day, - ((Datepart(WEEKDAY,DateField) +1) % 7) , DateField)) ) )as dfWeek
From Table
WHERE #StartingFriday <= DateField
) as aa
group by dfYear, dfWeek
order by dfYear desc, dfWeek desc
-- we want the weeknum of the (Friday on or before the DateField)
-- the % (percent sign) is the math MODULO operator.
-- used to get back to the nearest Friday,
-- day= Fri Sat Sun Mon Tue Wed Thu
-- weekday= 6 7 1 2 3 4 5
-- plus 1 = 7 8 2 3 4 5 6
-- Modulo7= 0 1 2 3 4 5 6
-- which are the days to subtract from DateField
-- to get to its Friday start of its week.
I did some testing with this
declare #dt as date = '8/17/18';
select ((DATEPART(WEEKDAY,#dt) +1) % 7) as wd
,(DATEADD(day, - ((Datepart(WEEKDAY,#dt) +1) % 7) , #dt)) as Fri
,(Datepart(Week,(DATEADD(day, - ((Datepart(WEEKDAY,#dt) +1) % 7) , #dt)) ) )as wk
,DATEADD(week,-9, (DATEADD(day, - ((Datepart(WEEKDAY,#dt) +1) % 7) , #dt)) ) as StartingFriday

Conditional scoring

I have a table that shows only the 'captured' data. For example in the below exhibit, the emp_no 17 has 2 records - for November and February (for a specified 6 month period, from July 2017). It does not have data for the other 4 months (within the 6-month date range, from previous 6 months to current date).
How can I populate these missing months (Sept, Oct, Dec) with default values for num_differences of 0 for the missing months? (for example, in this case, I want emp_no 17 to have the below (I can ignore 2018 data - only require data up to Dec 2017):
I have the script below:
declare #YMN date;
set #YMN = '20171201';
DECLARE #Emp TABLE (
[date] date,
[emp_no] int,
[num_differences] int
);
INSERT INTO #Emp VALUES
('2017-09-14', 17, 1), ('2017-12-01', 17, 1),('2017-12-18', 17, 1),('2017-12-21', 17, 1),
('2017-09-27', 17, 1), ('2017-12-04', 17, 1);
-------------------------------------------------------------------------------------------get missing dates---------------------------------------------------------------------------
;WITH cte_Emp_No AS (
SELECT DISTINCT [emp_no]
FROM #Emp
),
cte_dates AS (
SELECT [emp_no], DATEADD(month, -6, DATEADD(dd, -(DAY(dateadd(month, 1, #YMN)) - 1), dateadd(month, 1, #YMN))) AS [date]
FROM cte_Emp_No
UNION ALL
SELECT [emp_no], DATEADD(month, 1, [date]) AS [date]
FROM cte_dates
WHERE [date] < dateadd(month, 0, #YMN)
)
SELECT DISTINCT ISNULL(e.emp_no, c.emp_no) emp_no, ISNULL(e.date, c.date) date, ISNULL(e.num_differences, 0) num_differences
into ##new_table
FROM #Emp AS e
RIGHT JOIN cte_dates AS c ON YEAR(c.date) = YEAR(e.date) AND MONTH(c.date) = MONTH(e.date)
-----------------------------------------------------------------------------------------------MAIN CTE------------------------------------------------------------------------------
;with cte_RawScore as
(
select emp_no
, date YMN
,sum(case when datediff(month, convert(datetime, #YMN, 112), date) = 0 then num_differences else 0 end) as thismonth
,sum(case when datediff(month, convert(datetime, #YMN, 112), date) between -2 and 0 then num_differences else 0 end) as last3month
,sum(case when datediff(month, convert(datetime, #YMN, 112), date) between -5 and 0 then num_differences else 0 end) as last6month
from ##new_table d
group by emp_no, date
)
select
emp_no
,YMN
,case when last6month = 0 then 5
when last3month = 0 then 4
when thismonth = 0 then 3
when thismonth <= 3 then 2
else 1 end RawScore
from cte_RawScore
ORDER BY day(YMN) desc
drop table ##new_table
I want this the scoring only to be applicable for 6 months from and after July 2017. i.e. the #YMN is a variable that stores the year month number; and the score, according to the above rule applies to the 6 months from 201707.
So 201707 is 1 month,
201708 is 2 months, etc, up to 201712
I wish to have a list of employees with their associated scores, based on the rules mentioned below .
That’s, :
A score of 5 if 0 differences in 6 consecutive months ( from July to December) ;
A score of 4 if 0 differences in 3 consecutive months (from July to December);
A score of 3 if 0 differences for 1 month ( from July to December);
A score of 2 if 1 to 3 differences for 1 month (from July to December);
A score of 1 if 4 or more differences in 1 month (from July to December).
I get the number of differences from a table, but some employees do not appear for certain months; hence I want to give them a difference of 0 if they do not appear for that particular month.
Please assist.
I think I understand what you're getting at. Let me give you a simplified example. You need a table full of dates to join to. In data warehousing we use a Date dimension which has attributes about every date.
For your example your date dimension table could just have Month names or numbers:
1
2
...
12
Let's call this table Months.
Then you would do something like this, to count a zero for months with no data. Here I'm using what's called a Common Table Expression or CTE (the part with the WITH) in place of a table, since I'm not concerned with creating a permanent table right now.
WITH Months AS (
SELECT 1 AS MonthNumber UNION
SELECT 2 UNION
SELECT 3 UNION
SELECT 4 UNION
SELECT 5 UNION
SELECT 6 UNION
SELECT 7 UNION
SELECT 8 UNION
SELECT 9 UNION
SELECT 10 UNION
SELECT 11 UNION
SELECT 12
)
SELECT M.MonthNumber, COUNT(*)
FROM Months as M
LEFT JOIN MyData as D
ON MONTH(D.SomeDateValue) = M.MonthNumber
GROUP BY M.MonthNumber
This will guarantee every month appears with a count, perhaps of zero.

Microsoft SQL Calculating Backlog

I would like to calculate the backlog for every week in the past month. Date format is in (MM/DD/YY)
| mutation | issued_date | queryno_i | status |
-----------------------------------------------
01/05/14 12/31/13 321 OPEN
01/02/14 08/01/13 323 CLOSED
01/01/14 06/06/13 123 OPEN
01/01/14 01/01/14 1240 CLOSED
01/02/14 01/01/14 1233 OPEN
01/03/14 01/03/14 200 CLOSED
01/05/14 01/04/14 300 OPEN
01/06/14 01/05/14 231 OPEN
01/07/14 01/06/14 232 CLOSED
01/09/14 01/10/14 332 OPEN
01/11/14 01/11/14 224 CLOSED
01/15/14 01/14/14 225 CLOSED
01/16/14 01/15/14 223 OPEN
I want my result set to look like this:
WeekNum | Opened | Closed | Total Open
--------------------------------------
1 4 3 4 <= (2-4)+ data in week 2 so (2-4)+(1-2)+7
2 4 2 6 <= (1-2)+7
3 2 1 7 <= total count
My Code is below however I am not sure how to query the last part. I am not even sure if this is possible or not.
WITH
issued_queries AS
(
SELECT DATEPART(wk, issued_date) AS 'week_number'
,COUNT(queryno_i) AS 'opened'
FROM t.tech_query
WHERE DATEADD(D,-12,issued_date) > GETDATE()-40
GROUP BY DATEPART(wk, issued_date)
),
closed_queries AS
(
SELECT DATEPART(wk, mutation) AS 'week_number'
,COUNT(queryno_i) AS 'closed'
FROM t.tech_query
WHERE status=3 AND DATEADD(D,-12,issued_date) > GETDATE()-40
GROUP BY DATEPART(wk, mutation)
),
total as
(
SELECT COUNT(*) AS 'total'
FROM t.tech_query
WHERE status!='3'
)
SELECT issued_queries.week_number
, issued_queries.opened
, closed_queries.closed
FROM issued_queries JOIN closed_queries
ON (issued_queries.week_number = closed_queries.week_number)
ORDER BY week_number
Backlog for every week in the past month.
I've taken this to mean last 4 weeks, as that appears to be what you are doing.
Assuming "mutation" represents the date a record was updated (maybe set to closed).
So first, I generate a list of dates, so that way there will be an answer for week number X even if there are no new/closed records.
declare #SundayJustGone datetime
-- We need to get rid of the time component, done through convert.
set #SundayJustGone = convert(date, dateadd(d, 1-DATEPART(dw, getdate()), getdate()))
-- If earlier than sql 2008, can get rid of time component through: set #SundayJustGone = SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, #SundayJustGone))
;with
Last4Weeks as
(
-- Get the sunday of the week just gone.
select #SundayJustGone as SundayDate -- Sunday just gone
union all
select dateadd(d, -7, SundayDate) -- Get the previous Sunday
from Last4Weeks
where dateadd(d, -7, SundayDate) > dateadd(Wk, -4, #SundayJustGone) -- where the new date is not more than 4 weeks old
)
select A.SundayDate,
DATEPART(wk, DateAdd(d, -1, A.SundayDate)) as Week_Number, -- SQL considers Sunday the first day of the week, so we need to move it back 1 day to get the right week
(select count(*)
from t.tech_query
where issued_date between DateAdd(d, -6, A.SundayDate) and A.SundayDate -- Was issued this week. (between monday - sunday)
) as Opened,
(select count(*)
from t.tech_query
where status = 3 -- where it is closed
and mutation between DateAdd(d, -6, A.SundayDate) and A.SundayDate -- and the mutation was this week. (between monday - sunday)
) as Closed,
(select count(*)
from t.tech_query
where (status != 3 or datediff(d, mutation, A.SundayDate) < 0 ) -- Is still open, or was closed after this week.
and datediff(d, issued_date, A.SundayDate) >= 0 -- and it was issued on or before the sunday.
) as TotalOpen
from Last4Weeks as A
hopefully this helps.
the results are different to yours, as I assume Monday is the first day of the week. To change start of week back to sunday, saturday needs to be considered end of week, so, change the set #SundayJustGone = convert(date, dateadd(d, 1-DATEPART(dw, getdate()), getdate())) to set #SundayJustGone = convert(date, dateadd(d, -DATEPART(dw, getdate()), getdate())) (1 removed)

Labor Day Vs. Thanksgiving

I am creating a calendar table for my warehouse. I will use this as a foreign key for all the date fields.
The code shown below creates the table and populates it. I was able to figure out how to find Memorial Day (last Monday of May) and Labor Day (first Monday of September).
SET NOCOUNT ON
DROP Table dbo.Calendar
GO
Create Table dbo.Calendar
(
CalendarId Integer NOT NULL,
DateValue Date NOT NULL,
DayNumberOfWeek Integer NOT NULL,
NameOfDay VarChar (10) NOT NULL,
NameOfMonth VarChar (10) NOT NULL,
WeekOfYear Integer NOT NULL,
JulianDay Integer NOT NULL,
USAIsBankHoliday Bit NOT NULL,
USADayName VarChar (100) NULL,
)
ALTER TABLE dbo.Calendar ADD CONSTRAINT
DF_Calendar_USAIsBankHoliday DEFAULT 0 FOR USAIsBankHoliday
GO
ALTER TABLE dbo.Calendar ADD CONSTRAINT
DF_Calendar_USADayName DEFAULT '' FOR USADayName
GO
Declare #StartDate DateTime = '01/01/2000'
Declare #EndDate DateTime = '01/01/2020'
While #StartDate < #EndDate
Begin
INSERT INTO dbo.Calendar
(
CalendarId,
DateValue,
WeekOfYear,
DayNumberOfWeek,
NameOfDay,
NameOfMonth,
JulianDay
)
Values
(
YEAR (#StartDate) * 10000 + MONTH (#StartDate) * 100 + Day (#StartDate), --CalendarId
#StartDate, -- DateValue
DATEPART (ww, #StartDate), -- WeekOfYear
DATEPART (dw, #StartDate), -- DayNumberOfWeek
DATENAME (dw, #StartDate), -- NameOfDay
DATENAME (M, #StartDate), -- NameOfMonth
DATEPART (dy, #StartDate) -- JulianDay
)
Set #StartDate += 1
End
--=========================== Weekends
-- saturday and sunday
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Weekend, ' WHERE DayNumberOfWeek IN (1, 7)
--=========================== Bank Holidays
-- new years day
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'New Year''s Day, ' WHERE (CalendarId % 2000) IN (101)
-- memorial day (last Monday in May)
UPDATE dbo.Calendar
SET USAIsBankHoliday = 1,
USADayName += 'Memorial Day, '
WHERE 1=1
AND CalendarId IN
(
SELECT MAX (CalendarId)
FROM dbo.Calendar
WHERE MONTH (DateValue) = 5
AND DATEPART (DW, DateValue)=2
GROUP BY YEAR (datevalue)
)
-- independence day
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Independence Day, ' WHERE (CalendarId % 2000) IN (704)
-- labor day (first Monday in September)
UPDATE dbo.Calendar
SET USAIsBankHoliday = 1,
USADayName += 'Labor Day, '
WHERE 1=1
AND CalendarId IN
(
SELECT MIN (CalendarId)
FROM dbo.Calendar
WHERE MONTH (DateValue) = 9
AND DATEPART (DW, DateValue)=2
GROUP BY YEAR (datevalue)
)
-- thanksgiving day (fourth Thursday in November)
UPDATE dbo.Calendar
SET USAIsBankHoliday = 1,
USADayName += 'Thanksgiving Day, '
WHERE 1=1
AND CalendarId IN
(
SELECT Max (CalendarId)
FROM dbo.Calendar
WHERE MONTH (DateValue) = 11
AND DATEPART (DW, DateValue)=5
GROUP BY YEAR (datevalue)
)
-- christmas
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Christmas Day, ' WHERE (CalendarId % 2000) IN (1225)
--=========================== Other named days
-- new years eve
UPDATE dbo.Calendar SET USADayName += 'New Year''s Eve, ' WHERE (CalendarId % 2000) IN (1231)
-- christmas eve
UPDATE dbo.Calendar SET USADayName += 'Christmas Eve, ' WHERE (CalendarId % 2000) IN (1224)
-- boxing day
UPDATE dbo.Calendar SET USADayName += 'Boxing Day, ' WHERE (CalendarId % 2000) IN (1226)
--=========================== Remove trailing comma
UPDATE dbo.Calendar SET USADayName = SubString (USADayName, 1, LEN (USADayName) -1) WHERE LEN (USADayName) > 2
SELECT * FROM dbo.Calendar
I am stumped on figuring out Thanksgiving day (Thursday of the last FULL week of November).
Edit:
Correction based on comment by John Sauer
Thanksgiving is the fourth Thursday of November. However, upon checking several years, I find that it has turned out to also be the Thursday of the last full week of Nov.
I am stumped on figuring out Thanksgiving day (Thursday of the last FULL week of November).
Last Saturday of November - 2
Take the last Saturday of November, and subtract two days ;)
WITH cal AS
(
SELECT CAST('2009-30-11' AS DATETIME) AS cdate
UNION ALL
SELECT DATEADD(day, -1, cdate)
FROM cal
WHERE cdate >= '2009-01-11'
)
SELECT TOP 1 DATEADD(day, -2, cdate)
FROM cal
WHERE DATEPART(weekday, cdate) = 6
For the complex algorithms, it's sometimes better to find a matching date from a set than trying to construct an enormous single-value formula.
See this article in my blog for more detail:
Checking event dates
declare #thedate datetime
set #thedate = '11/24/2011'
select 1
where datepart(dw, #thedate) = 5 and datepart(m, #thedate) = 11 AND (datepart(dd, #thedate) - 21) between 0 and 6
Is the date a Thursday in November and is there less than a week remaining.
We use a neat Scalar-valued Function in our database that we can call to determine whether or not a date is a NonWorkDay. We manually add special dates for Observed Holidays.
Create Function:
CREATE FUNCTION [dbo].[NonWorkDay](#date as DATETIME)
RETURNS bit
AS
BEGIN
if #date is not null
begin
-- Weekends
if DATEPART(weekday, #date)=1 return 1 --Sunday
if DATEPART(weekday, #date)=7 return 1 --Saturday
-- JAN
if month(#date)=1 AND day(#date)=1 return 1 -- New Years Day
-- MAY
if month(#date)=5 and DATEPART(weekday, #date)=2 and day(#date)>24 and day(#date)<=31 return 1 --Memorial Day
-- JULY
if month(#date)=7 AND day(#date)=4 return 1 -- July 4th
-- SEP
if month(#date)=9 and DATEPART(weekday, #date)=2 and day(#date)<=7 return 1--Labor Day
-- NOV
if month(#date)=11 and DATEPART(weekday, #date)=5 and day(#date)>21 and day(#date)<=28 return 1--Thanksgiving
if month(#date)=11 and DATEPART(weekday, #date)=6 and day(#date)>22 and day(#date)<=29 return 1 -- Black Friday
-- DEC
if month(#date)=12 AND day(#date)=24 return 1 -- Christmas Eve
if month(#date)=12 AND day(#date)=25 return 1 -- Christmas
if month(#date)=12 AND day(#date)=31 return 1 -- NYE
-- Office Closed for Observed Holiday Dates
if month(#date)=7 AND day(#date)=5 AND year(#date)=2021 return 1 -- 4th of July Observed for 2021
if month(#date)=12 AND day(#date)=26 AND year(#date)=2022 return 1 -- Christmas Day observed for 2022
if month(#date)=1 AND day(#date)=2 AND year(#date)=2023 return 1 -- New Years Day observed for 2023
if month(#date)=7 AND day(#date)=3 AND year(#date)=2026 return 1 -- 4th of July Observed for 2026
if month(#date)=7 AND day(#date)=5 AND year(#date)=2027 return 1 -- 4th of July Observed for 2027
end
return 0
END
GO
Call Function:
IF [dbo].[NonWorkDay](Getdate())= 0 -- If (not a NonWorkDay -ie not Sat or Sun)

Resources