• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

How do I fix this code to push out date 7 business days so it doesn't include Sat & Sun?

I've attached SQL code that is currently moving our Orders out by 7 days.  However, I am VERY VERY new to SQL and have no idea how to parse out weekends (Sat/Sun) to move out the Ship Date by 7 Business Days instead of the simple 7 calendar days.

Thank you very much in advance to whoever can help me out.  
ISNDATES.sql
0
wshcraft70
Asked:
wshcraft70
  • 3
  • 3
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
you can use a function like the one of http://www.emoreau.com/Entries/Blogs/2009/01/SQL-A-UDF-User-defined-function-returning-all-days-between-2-dates.aspx to find all the dates in a range and extract weekends.
0
 
wshcraft70Author Commented:
@ emoreau

THANKS!!  However, I'm sooooooooo new to this that I haven't got a clue how I'd insert this/merge this into my existing code.  I put together my existing code with crops from other codes and honesly, am surprised I got it to work..  lol

Any suggestion on how I could merge it to the attached code would be fantastic..  THANKS IN ADVANCE!!
0
 
Éric MoreauSenior .Net ConsultantCommented:
First, you need to modify the UDF to return IDs:

ALTER FUNCTION [dbo].[GetAllDates] ( @StartDate DATETIME,  @EndDate DATETIME, @SkipWeekend BIT)
RETURNS @Dates TABLE ( ID int identity(1,1), CalendarDate DateTime)
AS BEGIN

      WHILE @StartDate <= @EndDate
      BEGIN  
            IF (@SkipWeekend = 0)   OR ((@SkipWeekend = 1) AND (DATEPART(weekday, @StartDate) NOT IN (1,7)))    
            BEGIN    
                  INSERT INTO @Dates(CalendarDate)  
                  SELECT @StartDate  
            END    
            SET @StartDate = DATEADD(dd, 1, @StartDate)
      END
      RETURN
END

Then you need to join the calendar twice. The first time to find the ID of your current date. The second time to find the ID of your date + 7.

Here is a sample:

SELECT *
FROM dbo.XRate AS X
left join dbo.GetAllDates('2010-11-01', '2010-11-30', 1) as AD1
on AD1.Calendardate = X.date
left join dbo.GetAllDates('2010-11-01', '2010-11-30', 1) as AD2
on AD2.ID = AD1.ID + 7
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
wshcraft70Author Commented:
My mind will totally have to work this one out!!  Thanks thought!!  It is the incorporation into my existing code that I just can't get!!  But I'm sure your solution is RIGHT ON..  Although I can read the code and know it is perfectly logical, I haven't even come close to mastering the building blocks of SQL..  Thank you very much!!
0
 
Éric MoreauSenior .Net ConsultantCommented:
is this query gives you the correct request date?

select *
From [001]..OEORDLIN_SQL AS LIN
inner join [001]..OEORDHDR_SQL AS HDR
on HDR.Ord_No = LIN.Ord_No
And HDR.Ord_Type = LIN.Ord_Type
And rtrim(ltrim(HDR.cus_no)) = '1070'
left join dbo.GetAllDates('2011-01-01', '2011-12-31', 1) as AD1
on AD1.Calendardate = LIN.Request_Dt
left join dbo.GetAllDates('2011-01-01', '2011-12-31', 1) as AD2
on AD2.ID = AD1.ID + 7
0
 
wshcraft70Author Commented:
The above runs without error in analyser.  But since I'm doing an UPDATE and SET to the LIN in 3 different fields:  LIN.Request_Dt, LIN.Promise_Dt, LIN.Req_Ship_Dt

Would I need to set this query on each date field to UPDATE and SET?  

Also, since I want this to go on "FOREVER" theoretically...  Would I need to set the GetALLDates to be something really far in the future?  

Below is my query which works great for just adding 7 days without eliminating the weekends.  Where to insert above query???

I totally appreciate your amazing help!!!  It is people like you that help us rookies to learn!  KUDOs!!

Begin Tran
Update HDR
Set Shipping_Dt = DateAdd(d, 7,
HDR.Shipping_Dt)
From [001]..OEORDHDR_SQL AS HDR
Where
rtrim(ltrim(HDR.cus_no)) = '1070'
--------
Update LIN
Set Request_Dt = DateAdd(d, 7, LIN.Request_Dt),
Promise_Dt = DateAdd(d, 7, LIN.Promise_Dt),
Req_Ship_Dt = DateAdd(d, 7, LIN.Req_Ship_Dt)
From [001]..OEORDLIN_SQL AS LIN, [001]..OEORDHDR_SQL AS HDR
Where HDR.Ord_No = LIN.Ord_No And HDR.Ord_Type = LIN.Ord_Type And
rtrim(ltrim(HDR.cus_no)) = '1070'



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now