SQL Date Extrapolation

Does anyone have a function that I could for instance pass the following:

   - Month
   - Year
   - Number between 1 and 5
   - DayofWeek (1=Sunday,2=Monday, etc...)

So I if I were to pass in 3,2005,1,1 the function would return the date of the first sunday in March of 2005

or

If I passed 3,2005,2,2 I would get a date which represented the 2nd Monday in March of 2005.  

If the info entered would retrun an error the function should return 'ERROR' or something like that.

I really need this function.
LVL 1
Steve GronerLead Systems EngineerAsked:
Who is Participating?
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.

mcmonapCommented:
Hi sgroner,

Check this out, I think it should do what you want, it is a bit lazy as I am relying on implicit conversion of the varchars to ints but it works okay:

CREATE FUNCTION dbo.specificDate(@month VARCHAR(2), @year CHAR(4), @occurance INT, @day INT)
RETURNS SMALLDATETIME
AS
BEGIN

DECLARE @count INT
DECLARE @date SMALLDATETIME

SET @count = 1
SET @date = @year+'-'+@month+'-01'

WHILE @count <= @occurance
BEGIN
      IF DATEPART(dw,@date) = @day
            IF @count = @occurance
                  RETURN @date
            ELSE
            BEGIN
                  SET @date = DATEADD(dd,1,@date)
                  SET @count = @count+1
            END
      ELSE SET @date = DATEADD(dd,1,@date)
END
RETURN @date
END
GO

SELECT dbo.specificDate(3,2005,2,2)
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
mcmonapCommented:
Hi sgroner,

Should have added that this will just keep going on the number of occurances so if you enter:
SELECT dbo.specificDate(3,2005,19,2)
It will return the 19 monday from the start of March 2005 (2005-07-11)

Also the function returns a small datetime since you don't appear to be worried about any time aspect of function.
0
Steve GronerLead Systems EngineerAuthor Commented:
Thanks for the function with a little modifcation I was able to get exactly what I needed.
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
Databases

From novice to tech pro — start learning today.

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.