Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Nth occurence of a day in a month/year


Nth occurence of a day in a month/year

Author
Message
Biplov Dan
Biplov Dan
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 32
Comments posted to this topic are about the item Nth occurence of a day in a month/year
michelle.baumgarten
michelle.baumgarten
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 21
Can't get this to work.
sknox
sknox
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2036 Visits: 2712
Why not just do (using the function definition and parameters (and the @firstDate variable) as given):

set @firstDate = dateadd(dd,1-datepart(dd,@inputdate) ,@inputDate) -- ensures that we start with the first of the month

select
dateadd(wk, @occurAt - 1, -- adds the additional weeks, if any
dateadd(dd, @day - datepart(dw, @firstDate) -- moves to the given day of week
+ case when @day < datepart(dw, dateadd(dd,1-datepart(dd,@firstdate) ,@firstDate)) then 7 else 0 end, -- ensures we don't go back to the last month
@firstDate
)
)


Biplov Dan
Biplov Dan
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 32
Can't get this to work.


How r u setting the parameter? Let me explain it.

For example if you need date for 3rd Sunday of March 2009 then use as follows

SELECT dbo.getDateAtNthOccurence('01/Mar/2009',1,3)

The output will be 2009-03-15 (15th march 2009)

The first parameter is any date of month/year for which we want the occurrence date.

The second parameter is as follows (if you need sunday then send 1 as parameter value):
1:Sunday, 2:Monday, 3:Tuesday, 4:Wednesday, 5:Thrusday, 6:Friday, 7:Saturday

The third parameter is as follows (if you need 3rd sunday then send 3 as parameter value):
1:First, 2:Second, 3:Third, 4:Fourth, >4:Last


hope this will help you to get the output.
cheers Smile
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45113 Visits: 39916
michelle.baumgarten (8/21/2009)
Can't get this to work.


The code works fine for me. You need to post the exact code that you can't get to work so we can help.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45113 Visits: 39916
sknox (8/21/2009)
Why not just do (using the function definition and parameters (and the @firstDate variable) as given):

set @firstDate = dateadd(dd,1-datepart(dd,@inputdate) ,@inputDate) -- ensures that we start with the first of the month

select
dateadd(wk, @occurAt - 1, -- adds the additional weeks, if any
dateadd(dd, @day - datepart(dw, @firstDate) -- moves to the given day of week
+ case when @day < datepart(dw, dateadd(dd,1-datepart(dd,@firstdate) ,@firstDate)) then 7 else 0 end, -- ensures we don't go back to the last month
@firstDate
)
)



I agree... the only desireable thing missing from the above code is for the "LAST" desired occurance. For example... for 2012-02-01 looking for Sunday(1) as the last occurance of a Sunday in the month (5), your good code returns the first Sunday of March instead of the last Sunday of February.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
michelle.baumgarten
michelle.baumgarten
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 21
Since you asked --- I copied the code, entered '01/MAR/2009',1,3 for the parameters, and got a message saying:
Incorrect syntax near keyword FUNCTION.
A RETURN statement with a RETURN value cannot be used in this context.


CREATE FUNCTION dbo.getDateAtNthOccurence
(
@inputDate DATETIME,
@day TINYINT, -- 1:Sunday, 2:Monday, 3:Tuesday, 4:Wednesday, 5:Thrusday, 6:Friday, 7:Saturday
@occurAt TINYINT -- 1:First, 2:Second, 3:Third, 4:Fourth, >4:Last
)
RETURNS DATETIME
AS
BEGIN

DECLARE @counter TINYINT,
@firstDate DATETIME,
@lastDate DATETIME,
@retDate DATETIME

SELECT @counter = 1, @firstDate = DATEADD(dd,-(DAY(@inputDate)-1),@inputDate), @lastDate = DATEADD(dd,-1,DATEADD(MONTH,1,@firstDate))

WHILE @firstDate<=@lastDate
BEGIN

IF(DATEPart(dw , @firstDate )=@day) and @counter<=@occurAt
BEGIN
SELECT @retDate = @firstDate,@counter = @counter+1
END

SET @firstDate = DATEADD(DAY,1,@firstDate)
END

-- Return the result of the function
RETURN @retDate

END
GO
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45113 Visits: 39916
michelle.baumgarten (8/24/2009)
Since you asked --- I copied the code, entered '01/MAR/2009',1,3 for the parameters, and got a message saying:
Incorrect syntax near keyword FUNCTION.
A RETURN statement with a RETURN value cannot be used in this context.


CREATE FUNCTION dbo.getDateAtNthOccurence
(
@inputDate DATETIME,
@day TINYINT, -- 1:Sunday, 2:Monday, 3:Tuesday, 4:Wednesday, 5:Thrusday, 6:Friday, 7:Saturday
@occurAt TINYINT -- 1:First, 2:Second, 3:Third, 4:Fourth, >4:Last
)
RETURNS DATETIME
AS
BEGIN

DECLARE @counter TINYINT,
@firstDate DATETIME,
@lastDate DATETIME,
@retDate DATETIME

SELECT @counter = 1, @firstDate = DATEADD(dd,-(DAY(@inputDate)-1),@inputDate), @lastDate = DATEADD(dd,-1,DATEADD(MONTH,1,@firstDate))

WHILE @firstDate<=@lastDate
BEGIN

IF(DATEPart(dw , @firstDate )=@day) and @counter<=@occurAt
BEGIN
SELECT @retDate = @firstDate,@counter = @counter+1
END

SET @firstDate = DATEADD(DAY,1,@firstDate)
END

-- Return the result of the function
RETURN @retDate

END
GO


I suspect that you're changing the code above and then trying to run it as if it were a script. It will not work that way. Run the code as it is above to create the function and then run something like the following to use the function...

SELECT dbo.getDateAtNthOccurence(GETDATE(),1,5)



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search