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

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024
  • We can add days fairly easily in SQL Server. But what about the next working day?
    My SQL Server Udemy courses are:
    70-461, 70-761 Querying Microsoft SQL Server with T-SQL: rebrand.ly/que...
    98-364: Database Fundamentals (Microsoft SQL Server): rebrand.ly/dat...
    70-462 SQL Server Database Administration (DBA): rebrand.ly/sql...
    Microsoft SQL Server Reporting Services (SSRS): rebrand.ly/sql...
    SQL Server Integration Services (SSIS): rebrand.ly/sql...
    SQL Server Analysis Services (SSAS): rebrand.ly/sql...
    Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): rebrand.ly/mic...
    ----
    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')
    FROM sys.objects AS O1
    CROSS JOIN sys.objects AS O2
    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"
    =@myDate)
    ----
    Links to my website are:
    70-461, 70-761 Querying Microsoft SQL Server with T-SQL: idodata.com/que...
    98-364: Database Fundamentals (Microsoft SQL Server): idodata.com/dat...
    SQL Server Essential in an Hour: idodata.com/sql...
    70-462 SQL Server Database Administration (DBA): idodata.com/sql...
    DP-300: Administering Relational Databases: idodata.com/dp-...
    Microsoft SQL Server Reporting Services (SSRS): idodata.com/mic...
    SQL Server Integration Services (SSIS): idodata.com/sql...
    SQL Server Analysis Services (SSAS): idodata.com/sql...
    Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): rebrand.ly/mic...
    1Z0-071 Oracle SQL Developer - certified associate: idodata.com/iz0...
    SQL for Microsoft Access: idodata.com/sql...
    DP-900: Microsoft Azure Data Fundamentals: idodata.com/dp-...

Комментарии • 2

  • @victorzuniga8894
    @victorzuniga8894 10 месяцев назад +1

    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!

  • @saktibiswal6445
    @saktibiswal6445 10 месяцев назад +1

    Awesome Explanation!!