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
LVL 1
wshcraft70Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

É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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.