Technical Article

UDF_GET_NEXT_WEEKDAY

,

How To Use:

SELECT dbo.UDF_GET_NEXT_WEEKDAY('20111223', 'SUNDAY')

This is the straight fwd and easy understanding way to retrieve the specified date..

We can similarly, get the specific date , which is preceded by a given date by changing  line of code from  (line no:13)

Select dayid, DATEADD(day, 1*dayid, @in_date) as possibledate

to

Select dayid, DATEADD(day, -1*dayid, @in_date) as possibledate

create FUNCTION UDF_GET_NEXT_WEEKDAY
(
@in_date datetime,
@in_desired_weekday sysname --it can be monday/tuesday, wednesday, thursday, friday, saturday
)
returns datetime
AS
BEGIN
deClare @out_datetime datetime
Select  @out_datetime = possibledate
from
(
Select dayid, DATEADD(day, 1*dayid, @in_date) as possibledate
from
(
select 1 as dayid
union all
select 2 as dayid
union all
select 3 as dayid
union all
select 4 as dayid
union all
select 5 as dayid
union all
select 6 as dayid
union all
select 7 as dayid
) weekdays
)src
where 
 datename(weekday, possibledate)= @in_desired_weekday
RETURN @out_datetime
END
go
/*
How To Use:

SELECT dbo.UDF_GET_NEXT_WEEKDAY('20111223', 'SUNDAY')
*/

Rate

3.5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

3.5 (2)

You rated this post out of 5. Change rating