SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Nth occurrence of a weekday in a month and year


Nth occurrence of a weekday in a month and year

Author
Message
rajneeshkumardev
rajneeshkumardev
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 16
Comments posted to this topic are about the item Nth occurrence of a weekday in a month and year
Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1020 Visits: 653
IF OBject_ID('getNthOccuranceOfWeekDay') IS NOT NULL
BEGIN
DROP FUNCTION dbo.getNthOccuranceOfWeekDay
END

GO

CREATE FUNCTION dbo.getNthOccuranceOfWeekDay
(
@year VARCHAR(4),
@month TINYINT,
@weekday TINYINT,
@iOccAt TINYINT
)
RETURNS DATETIME
AS
BEGIN

DECLARE @dt DATETIME, @dtNextMonth DATETIME,@retDate DATETIME,@Wk TINYINT

SELECT
@dt = DATEADD(mm,@month-1,@year),--CAST(@year AS CHAR(4))+'-'+CAST(@month AS CHAR(2))+'-01'
@dtNextMonth = DATEADD(mm,@month,@year),
@Wk = CASE WHEN DATEPART(DW,@dt ) IN (7,1) THEN DATEDIFF(WK,@dt,@dtNextMonth) ELSE DATEDIFF(WK,@dt,@dtNextMonth) -1 END

SELECT @iOccAt = CASE WHEN @iOccAt > @wk THEN @wk-1 ELSE @iOccAt-1 END


SELECT
@dt =
CASE WHEN DATEPART(DW,@dt)> @Weekday THEN DATEADD(dd, 7*@iOccAt, DATEADD(dd, @weekday - DATEPART(DW,@dt)+ 7 , @dt))
WHEN DATEPART(DW,@dt)< @Weekday THEN DATEADD(dd, 7*@iOccAt,DATEADD(dd, @weekday - DATEPART(DW,@dt) , @dt) )
ELSE @dt
END

RETURN @dt
END
GO

Regards,
Mitesh OSwal
+918698619998
jordonpilling
jordonpilling
Old Hand
Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)

Group: General Forum Members
Points: 300 Visits: 451
Working great for 1,2,3 & 4th occurances, but if i pass in 5 for april 2010, i am getting the wrong date

For example:
SELECT dbo.getNthOccuranceOfWeekDay(YEAR(GETDATE()), MONTH(GETDATE()), 1, 5)



Resulting in:
2010-04-18 00:00:00.000


Should be:
2010-04-25 00:00:00.000
(because the 25th is the last occurance of sunday in April.

Please advise.

Edit: my above issue occurs with Mitesh Oswal's version only, the version from Rajneesh Kumardev does not suffer from this problem.

Knock Knock, Who's There?, sp underscore, sp underscore who?
spid1, spid2, spid3....
Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1020 Visits: 653
DECLARE @year VARCHAR(4),
@month TINYINT,
@weekday TINYINT,
@iOccAt TINYINT

DECLARE @dt DATETIME

SELECT @iOccAt = 5,@weekday =6,@month = 5,@year = 2014
select @dt= DATEADD(MM,@month-1,DATEADD(YYYY,@year-1900,0))+
CASE WHEN DATEPART(DW,DATEADD(MM,@month-1,DATEADD(YYYY,@year-1900,0))) > @weekday
THEN 7-DATEPART(DW,DATEADD(MM,@month-1,DATEADD(YYYY,@year-1900,0)))+@weekday
ELSE @weekday - (DATEPART(DW,DATEADD(MM,@month-1,DATEADD(YYYY,@year-1900,0)))) END + (7*(@iOccAt-1))

select @dt, DATENAME(Dw,@dt)

Regards,
Mitesh OSwal
+918698619998
Iwas Bornready
Iwas Bornready
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18306 Visits: 885
Thanks for the script. I like date sql.
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