Solved

SQL Date Extrapolation

Posted on 2005-03-15
Medium Priority
393 Views
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.
0
Question by:Steve Groner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 15

Accepted Solution

mcmonap earned 1000 total points
ID: 13553179
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 @count = @count+1
END
END
RETURN @date
END
GO

SELECT dbo.specificDate(3,2005,2,2)
0

LVL 15

Expert Comment

ID: 13554296
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

LVL 1

Author Comment

ID: 13558254
Thanks for the function with a little modifcation I was able to get exactly what I needed.
0

Featured Post

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll