How can I exclude weekends and holidays in SQL Server query

christina G

I'm creating a query which will adjust the date excluding holidays and weekends.

Example data:

Adjusted Date | Adjusted Date(Excluding Holidays and weekends)

02/06/16 | 02/09/16

On my example, The date is a weekend and adjusted date becomes Feb 9 because Feb 8 is a holiday, so it needs to adjust so that the adjusted date would be a working day. Currently, I have a separated table of all the weekends and holidays in a fiscal year.

select  case when (
               select   count(dbo.WeekendsHoliday.[Weekends & Holidays])
               from     dbo.WeekendsHoliday
               where    dbo.WeekendsHoliday.[Weekends & Holidays] 
                    = case when convert(time, [Time Received]) > convert(time, '5:00:00 PM') 
                           then dateadd(day, 1, [Date Received]) 
                           else [Date Received] 
                      end
              ) > 0 
         then case (datename(DW, 
                             case when convert(time, [Time Received]) > convert(time, '5:00:00 PM') 
                                  then dateadd(day, 1, [Date Received])
                                  else [Date Received] 
                             end))
                when 'Saturday'
                then dateadd(day, 2, 
                             case when convert(time, [Time Received]) > convert(time, '5:00:00 PM') 
                                  then dateadd(day, 1, [Date Received])
                                  else [Date Received] 
                             end)
                else dateadd(day, 1, 
                             case when convert(time, [Time Received]) > convert(time, '5:00:00 PM') 
                                  then dateadd(day, 1, [Date Received])
                                  else [Date Received] 
                             end)
              end
    end as [Adjusted Date Excluding holidays and weekends]

What happens here is if the holiday is 2 consecutive days (Thursday and Friday), adjusted date would be Saturday which is still not valid because it's a weekend.

Adjusted date is an alias here

Balde

I suggest to create a function that recursively verify the next working day based on the table that contains weekends and holidays. The advantage of this approach is that it is a reusable function whenever you need it.

This function receives the date and time. (Based on the code in your question) if the time is after 5pm, adds a day. After, continues checking if the date is not within weekends or holidays until find the next working day:

CREATE FUNCTION dbo.adjustedDate(@dateReceived DATETIME, @timeReceived TIME)
RETURNS DATETIME
AS
BEGIN
    DECLARE @adjustedDate DATETIME = @dateReceived

    -- Verify time to add 1 day to @adjustedDate
    IF @timeReceived IS NOT NULL
        IF @timeReceived > CONVERT(TIME, '5:00:00 PM')
             SET @adjustedDate = DATEADD(DAY, 1, @adjustedDate)

    -- Continue adding 1 day to @adjustedDate recursively until find one date that is not a weekend or holiday
    IF EXISTS(SELECT [Weekends & Holidays]
                FROM dbo.WeekendsHoliday
                WHERE [Weekends & Holidays] = @adjustedDate)                
        SET @adjustedDate = dbo.adjustedDate(DATEADD(DAY, 1, @adjustedDate), NULL)

    RETURN @adjustedDate
END

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I show the table structure in SQL Server query?

From Dev

How can I modify this SQL query to exclude all results except from the previous two hours?

From Dev

How can I stop SQL Server query from returning multile rows for the same item ID in this query

From Dev

How to count days excluding weekends and holidays in Emacs calendar

From Dev

How can I query a SQL Server XML column and return all values for a specific node?

From Dev

How do I exclude weekends in SQL?

From Dev

Exclude / ignore weekends in Oracle SQL

From Dev

How do you exclude another table from a query in SQL Server?

From Dev

How I can get table definition in SQL SERVER 2008 R2 using SQL query?

From Dev

How I can group the results by day in this query with SQL Server?

From Dev

How can I make this query recursive Sql Server?

From Dev

Need to delete three consecutive days excluding weekends and holidays in sql

From Dev

How to count days excluding weekends and holidays in Emacs calendar

From Dev

How can I exclude files

From Dev

SQL Server: I need to manipulate data differently on weekends than weekdays

From Dev

Exclude weekends days from the holidays

From Dev

include weekends on sql query

From Dev

How can I add days in a date excluding weekends and holidays

From Dev

How can I convert this query result to date format (SQL Server)

From Dev

SQL Query to find the past 3rd business day excluding holidays and weekends

From Dev

How can I write a query in SQL Server on XML column

From Dev

How can I find the number of weekends betwee 2 dates

From Dev

How can I write this SQL Server query?

From Dev

Exclude weekends from my query

From Dev

SQL Server How to get Date Difference excluding Weekends and Holidays?

From Dev

how can I write a query (SQL Server) that, based on a table, it can return all the tables that it references?

From Dev

How can I split values returned by sql query into multiple columns in SQL SERVER?

From Dev

How can I display tablenames in resultheader of SQL Server 2008 query

From Dev

SQL - How to exclude weekends from date range using 'datediff'

Related Related

  1. 1

    How can I show the table structure in SQL Server query?

  2. 2

    How can I modify this SQL query to exclude all results except from the previous two hours?

  3. 3

    How can I stop SQL Server query from returning multile rows for the same item ID in this query

  4. 4

    How to count days excluding weekends and holidays in Emacs calendar

  5. 5

    How can I query a SQL Server XML column and return all values for a specific node?

  6. 6

    How do I exclude weekends in SQL?

  7. 7

    Exclude / ignore weekends in Oracle SQL

  8. 8

    How do you exclude another table from a query in SQL Server?

  9. 9

    How I can get table definition in SQL SERVER 2008 R2 using SQL query?

  10. 10

    How I can group the results by day in this query with SQL Server?

  11. 11

    How can I make this query recursive Sql Server?

  12. 12

    Need to delete three consecutive days excluding weekends and holidays in sql

  13. 13

    How to count days excluding weekends and holidays in Emacs calendar

  14. 14

    How can I exclude files

  15. 15

    SQL Server: I need to manipulate data differently on weekends than weekdays

  16. 16

    Exclude weekends days from the holidays

  17. 17

    include weekends on sql query

  18. 18

    How can I add days in a date excluding weekends and holidays

  19. 19

    How can I convert this query result to date format (SQL Server)

  20. 20

    SQL Query to find the past 3rd business day excluding holidays and weekends

  21. 21

    How can I write a query in SQL Server on XML column

  22. 22

    How can I find the number of weekends betwee 2 dates

  23. 23

    How can I write this SQL Server query?

  24. 24

    Exclude weekends from my query

  25. 25

    SQL Server How to get Date Difference excluding Weekends and Holidays?

  26. 26

    how can I write a query (SQL Server) that, based on a table, it can return all the tables that it references?

  27. 27

    How can I split values returned by sql query into multiple columns in SQL SERVER?

  28. 28

    How can I display tablenames in resultheader of SQL Server 2008 query

  29. 29

    SQL - How to exclude weekends from date range using 'datediff'

HotTag

Archive