Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SQL Date Extrapolation

Posted on 2005-03-15
3
Medium Priority
?
403 Views
Last Modified: 2008-03-06
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
Comment
Question by:Steve Groner
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
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 @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
 
LVL 15

Expert Comment

by:mcmonap
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

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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

580 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