How to find preceding 7th date of any given date.

  • Hi,

    My requirement is,I want preceding 7th date of any given date.

    For example,

    1)Declare @AsonDate As datetime = '2013-08-31'

    Then my output should be '2013-08-07'

    2)Declare @AsonDate As datetime = '2013-09-06'

    Then my output should be '2013-08-07'

    Kindly help me!!

    Thanks in Advance!!

  • Here you go:

    DECLARE @AsonDate DATETIME = '2013-08-11';

    SELECT CASE WHEN DAY(@AsonDate) < 7

    THEN DATEADD(d,6,DATEADD(d,-(DAY(DATEADD(m,-1,@AsonDate))-1),DATEADD(m,-1,@AsonDate)))

    ELSE DATEADD(d,6,DATEADD(d,-(DAY(@AsonDate) - 1),@AsonDate))

    END;

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Thanks!!

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

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