Calculate the next working day (excluding weekends and vacation/holidays) in SQL Server

preview_player
Показать описание
We can add days fairly easily in SQL Server. But what about the next working day?
My SQL Server Udemy courses are:
----
In this video, I'll show you how to calculate the next working day (excluding weekends and vacation/holidays) in SQL Server. This video is perfect for students and professionals who need to schedule appointments or manage their work schedule.

I'll walk you through the steps needed to calculate the next working day in SQL Server, and I'll provide examples to make the process easier to understand. You'll be able to use this information to manage your work schedule and avoid conflicts with other commitments. So don't wait any longer, watch this video and learn how to calculate the next working day in SQL Server!

The end code is (replace "Less Than" with the appropriate symbol):
DROP TABLE IF EXISTS DateTable;
DROP TABLE IF EXISTS Holidays;

CREATE TABLE DateTable
(Dates Date)
INSERT INTO DateTable
SELECT DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)), '2009-12-31')

CREATE TABLE Holidays
(Holiday date)

INSERT INTO Holidays
VALUES ('2023-12-25'),('2023-12-26'),('2024-01-01')

--SELECT *
--FROM DateTable
--WHERE DATEPART(WEEKDAY, Dates) NOT IN (7, 1)

DECLARE @myDate date = '2023-12-24';

With LEADTable AS (
SELECT Dates, LEAD(Dates, 3) OVER(ORDER BY Dates) as ThreeDaysLater
FROM DateTable
WHERE DATEPART(WEEKDAY, Dates) NOT IN (7, 1)
AND Dates NOT IN (SELECT Holiday FROM Holidays)
)
SELECT *
FROM LEADTable
WHERE Dates = (SELECT MAX(Dates) from DateTable
WHERE DATEPART(WEEKDAY, Dates) NOT IN (7, 1)
AND Dates NOT IN (SELECT Holiday FROM Holidays)
AND Dates
Replace with "Less Than"
----
Links to my website are:
Рекомендации по теме
Комментарии
Автор

I have discovered this channel and it is amazing! Thank you. I want to suggest you something:

- I have found trouble while installing SQL Server again after uninstalling it; a lot of issues came out. Not only that, but I would like you to do a video explaining the best way to fully uninstall SQL Server from the computer, so we can reinstall it successfully without errors. I had to fully format my computer to install it again, I could not find any place that provides information about uninstallation. Furthermore, I would appreciate it if you do so. Thanks!

victorzuniga