Calculating the Second Sunday jus giving the month alone

  • Folks,

    Hwo to Calculate the second sunday of the particular month

    Example -- the user input is month alone like... 08 the second sunday is 09 Likewise.

    Can u please help me out with this.

    Happy Querying

  • Hi,

    Just modify David Burrows coding posted in the same question early

    SET DATEFIRST 1

    CREATE function get_2_sunday

    (

    @month int,

    @year int

    )

    RETURNS INT

    as

    begin

    declare @day int

    SELECT @day = DAY(DATEADD(day,7+7-DATEPART(dw,(cast(@year as varchar)+'-'+cast(@month as varchar)+'-'+'01')),

    (cast(@year as varchar)+'-'+cast(@month as varchar)+'-'+'01')))

    RETURN ( @day )

    END

    select dbo.get_2_sunday (2,2008)

  • David,

    It was returning an error

    ERROR -Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '8'.

    while executing the function

  • Hi,

    complie this statement

    and getback with the result

    declare @month int,@year int,@DATE varchar(15)

    select @month = 8,@year = 2009

    set @DATE = (cast(@year as varchar)+'-'+cast(@month as varchar)+'-'+'01')

    select @DATE

    SELECT DAY(DATEADD(day,7+7-DATEPART(dw,(cast(@year as varchar)+'-'+cast(@month as varchar)+'-'+'01')),

    (cast(@year as varchar)+'-'+cast(@month as varchar)+'-'+'01')))

Viewing 4 posts - 1 through 3 (of 3 total)

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