简体   繁体   中英

Add fixed number of days to date column in SQL Server table and determine weekends

I have a table, say Table 1, in SQL Server as given below:

Location   No:of Days
 Timbuktu     7
 Valhalla     5
   Spain      20

This table lists locations with number of days to deliver to these locations.

I have another table, Table2 as given below:

Location     Part     Ready_To_Ship_Date     Estimated_Delivery_Date

Valhalla     XXXX           26-Dec-15    
  Spain      YYYY           01-Jan-16
Valhalla     ZZZZ           15-Jan-16
Timbuktu     AAAA           15-Dec-15

Now I need to add the number of days for a location given in Table 1 to the corresponding Ready_To_Ship dates of records for that location in Table 2, get Estimated_Delivery_Date and check if the delivery date falls on a Saturday or Sunday. If yes, then put date as the following Monday. For a few locations, this will be Friday and Saturday as you all know weekends around the world are not the same.

I found this thread in SO: With SQL Server add x number of days to a date in sql but also pass in the number of business days in a week

But I am not able to figure out how to use it for my use case. Kindly guide me on this one.

Thank you in advance for the help offered.

You could take a two-step approach. Not the most efficient but it might be good enough for you. Change the values in the second update to correspond to your desired weekend days:

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        (select "No:of Days" from Table1 t1 where t1.Location = Table2.Location),
        Ready_To_Ship_Date
    );

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        case
            when datepart(weekday, Estimated_Delivery_Date) = 7 then 2
            when datepart(weekday, Estimated_Delivery_Date) = 1 then 1
            else 0 /* redundant with the where */
        end,
        Estimated_Delivery_Date
    )
where datepart(weekday, Estimated_Delivery_Date) in (1, 7);

That's not to say it can't be done in a single step; it just gets harder to read:

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        coalesce((select "No:of Days" from Table1 t1 where t1.Location = Table2.Location), 0)
        + case
            datepart(weekday, dateadd(
                day,
                (select "No:of Days" from Table1 t1 where t1.Location = Table2.Location),
                Ready_To_Ship_Date
            ))
            when 7 then 2
            when 1 then 1
            else 0
        end,
        Ready_To_Ship_Date
    )
    end

That second query also handles a failed lookup in Table1 via the coalesce() . I don't get the impression you'll have that problem.

Also note that some people would use an inner join syntax in the update rather duplicate subqueries. That's non-standard though and it has a lot of quirks which make it a good thing to avoid. And the optimizer can probably still sort it all out.

Now if you need the weekend day calculations to by dynamic across the various countries you'll need to have a look-up for that data somewhere. It also can depend on the regional settings of your server if you don't find a generic solution.

You might prefer something more robust but a simple option is to suppose you add columns WeekendDay1 and WeekendDay2 to Table1 that use US day numbering and a server setting of @@datefirst that matches.

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        coalesce((select "No:of Days" from Table1 t1 where t1.Location = Table2.Location), 0)
        + case
            datepart(weekday, dateadd(
                day,
                (select "No:of Days" from Table1 t1 where t1.Location = Table2.Location),
                Ready_To_Ship_Date
            ))
            when (select WeekendDay1 from Table1 t1 where t1.Location = Table2.Location)
              then 2
            when (select WeekendDay2 from Table1 t1 where t1.Location = Table2.Location)
              then 1
            else 0
        end,
        Ready_To_Ship_Date
    )
    end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM