The start week is Saturday and end week is Friday, I'd like to write a function to return the end week of a date.
e.g.
'2014-6-26' (Thursday) would return '2014-6-27' (Friday)
'2014-6-27' (Friday) would return '2014-6-27' (same day)
'2014-6-28' (Sat) would return '2014-7-04' (next Friday)
Because I cannot SET DATEFIRST in functions, I am struggling to create this function.
This is what I got so far. It works in all scenarios except when I enter a Saturday it returns the previous Friday (which is wrong). e.g. '2014-6-28' returns '2014-6-27'
CREATE FUNCTION [GetEndWeek](
@Date DATETIME
)
RETURNS DATETIME
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN DATEADD(DAY,(13 - (@@DATEFIRST + DATEPART(WEEKDAY, @Date )))%7,@Date )
END
Any help appreciated.
I do not want to SET DATEFIRST anywhere else (i.e. in procs) as I'm calling this function a lot.
Thanks!