Last Sunday

  • Comments posted to this topic are about the item Last Sunday

  • The function below returns the week ending date for the specified week day.

    -- =============================================

    -- Author: Barkha Javed

    -- Create date: 10 Jul 2009

    -- Description: Date of week ending, per given weekday

    -- =============================================

    ALTER FUNCTION [dbo].[fn_LastWeek]

    ( @Duration int,

    @DayOfWeek varchar(10) = Null

    )

    RETURNS nchar(8)

    AS

    BEGIN

    DECLARE @dt nchar(8)

    DECLARE @DiffDays int

    SELECT @DiffDays = CASE WHEN @DayOfWeek Like 'Mon%' THEN '2'

    WHEN @DayOfWeek Like 'Tue%' THEN '3'

    WHEN @DayOfWeek Like 'Wed%' THEN '4'

    WHEN @DayOfWeek Like 'Thu%' THEN '5'

    WHEN @DayOfWeek Like 'Fri%' THEN '6'

    WHEN @DayOfWeek Like 'Sat%' THEN '7'

    WHEN @DayOfWeek Like 'Sun%' THEN '1'

    ELSE '1' END

    SELECT @dt = Convert(nchar(8),DateAdd(d, -(DatePart(w, getdate())-@DiffDays), DateADD(wk, @Duration, getdate())),1)

    RETURN @dt

    END

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply