subtract 1 month from the year and month.

  • subtract 1 month from the year and month.

    I have @yearmonth='2018-03' .i have data -'2018-03' and i want to have '2018-02' .it should work for last 4-5 months. I should be able to get to 2017-10 etc.
    please help. Thanks.

  • Papil - Wednesday, April 25, 2018 12:42 PM

    subtract 1 month from the year and month.

    I have @yearmonth='2018-03' .i have data -'2018-03' and i want to have '2018-02' .it should work for last 4-5 months. I should be able to get to 2017-10 etc.
    please help. Thanks.

    This help?

    DECLARE @yearmonth VARCHAR(7) ='2018-03' ;

    SELECT CONVERT(VARCHAR(7),DATEADD(MONTH, -1, @yearmonth + '-01'),121);

  • Lynn Pettis - Wednesday, April 25, 2018 12:49 PM

    Papil - Wednesday, April 25, 2018 12:42 PM

    subtract 1 month from the year and month.

    I have @yearmonth='2018-03' .i have data -'2018-03' and i want to have '2018-02' .it should work for last 4-5 months. I should be able to get to 2017-10 etc.
    please help. Thanks.

    This help?

    DECLARE @yearmonth VARCHAR(7) ='2018-03' ;

    SELECT CONVERT(VARCHAR(7),DATEADD(MONTH, -1, @yearmonth + '-01'),121);

    Yes thanks.

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

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