I am trying to subtract the date from the given date. While subtracting if a weekend (Saturday & Sunday) is in-between, I need to ignore and subtract.
If my input is "2023.03.20" and I'm subtracting 3 days, the expected output would be "2023.03.15". But in my case it's returning "2023.03.17".
I was trying like
DECLARE @datum date
DECLARE @CalculatedTargetDate date
SET @datum =
CASE
WHEN DATENAME(DW, CONVERT(DATETIME, '2023-03-20', 102)) = 'Saturday'
THEN (CONVERT(DATETIME, '2023-03-20', 104) -1)
WHEN DATENAME(DW, CONVERT(DATETIME, '2023-03-20', 102)) = 'Sunday'
THEN (CONVERT(DATETIME, '2023-03-20', 104) -2)
ELSE
'2023-03-20'
END
SET @CalculatedTargetDate = DATEADD(day, -3, @datum )
select @CalculatedTargetDate