Solved

Convert VBA into User Defined Function (SQL Server 2000)

Posted on 2004-03-22
4
354 Views
Last Modified: 2011-10-03
Please provide working source code for the following, such that I can call a User Defined Function and get the same results as the VBA functions:


VBA Function:

Function ForwardDays(intDays As Long, Optional datStartDate As Date) As Date
Dim X As Long
    If datStartDate = 0 Then
        datStartDate = Date
    End If
For X = 1 To intDays
datStartDate = NextWorkDay(datStartDate)
Next X
ForwardDays = datStartDate
End Function

Function NextWorkDay(Optional datDate As Date) As Date
    If datDate = 0 Then
        datDate = Date
    End If
    Select Case Weekday(datDate)
    Case 1 To 5
    NextWorkDay = datDate + 1
    Case 6
    NextWorkDay = datDate + 3
    Case 7
    NextWorkDay = datDate + 2
    End Select
End Function



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

User Defined Function equivalent:


???????????


???????????


0
Comment
Question by:knowlton
  • 2
4 Comments
 
LVL 5

Author Comment

by:knowlton
ID: 10653894
Sample call to Forward Days:


?ForwardDays(5,#3/22/2004#)
3/29/2004


So it is 5 business days from today  (skip the weekend)
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 400 total points
ID: 10654511
Unfortunately, since GetDate() (the SQL equivalent of Date) is nod-deterministic you cannot use it in a User Defined Function.  Hence you need to pass the start date in.

This function returns a date the number business days specified from the start date.
CREATE FUNCTION getNextDate
      (@NumDays as int,
      @StartDate as datetime)
             
RETURNS datetime
AS
begin
      declare @DateRet datetime
      declare @Count int

      SET @Count = 0
      SET  @DateRet  =@StartDate
      WHILE(@Count < @NumDays)
      BEGIN
            SET  @DateRet  = DateAdd(d, 1, @DateRet)
                         if( DATEPART(dw, @DateRet) in (2,3,4,5,6))
                  SET @Count = @Count + 1
      END
      return(convert(varchar, @DateRet,101))
end
0
 
LVL 2

Assisted Solution

by:dhenson
dhenson earned 100 total points
ID: 10654562
This one returns the next work day after the date passed in. Was working on it before dfiala posted so i'll toss it in the mix.

CREATE FUNCTION NextWorkDay
 (@datDate datetime)  
RETURNS datetime AS  
BEGIN
declare @NextWorkDay datetime
set @NextWorkDay =
      case
            when datepart(dw,@datdate) between 1 and 5 then @datdate + 1
            when datepart(dw,@datdate) = 6 then @datdate + 3
            when datepart(dw,@datdate) = 7 then @datdate + 2
      end
return @NextWorkDay
END


0
 
LVL 5

Author Comment

by:knowlton
ID: 10659099
Thank you both.

This is just part of a larger conversion I am doing from Access over to SQL Server.

I may just have the DBA write it.

But this helps.

Tom
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now