﻿-- =============================================
-- Author:		Dr. Stefan Ott
-- Create date: 31.01.2013
-- Description:	first day of week
-- =============================================
CREATE FUNCTION [dbo].[date_weekend]
(
	@TargetDate date
)
RETURNS date
AS
BEGIN
	
	DECLARE @Corretor smallint = -((@@DATEFIRST % 7) - 1);
	DECLARE @ret date = CASE WHEN @TargetDate is null THEN null ELSE dateadd(d ,-((datepart(weekday,@TargetDate) - 1 - @Corretor)%7), @TargetDate) END;
	return DATEADD(DAY, 6, @ret);
END