if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_Get_WeekDay]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_Get_WeekDay]
GO
/*
Purpose: Get First or Last Week Day
where @ToDay is interested date
@MonthIncrement is increment (For example: -1 is previous week;
0 is current week;
1 next week and so on
@FirstLast: 'F' is First;
'L' is Last)
*/
CREATE FUNCTION dbo.f_Get_WeekDay( @ToDay datetime ,
@MonthIncrement int,
@FirstLast char(1))
RETURNS datetime
AS
BEGIN
set @ToDay = convert(datetime, convert( varchar(30), @ToDay, 103), 103)
return case @FirstLast
when 'F' then @ToDay
- dbo.f_GetProperWeekDay(DATEPART ( dw , @ToDay )) + 1
+ @MonthIncrement * 7
when 'L' then @ToDay
+ 7 - dbo.f_GetProperWeekDay(DATEPART ( dw , @ToDay ))
+ @MonthIncrement * 7
else null
end
END
GO