select last 30 days

  • hi Friends

    How can i select last 30 days excluding sundays

    Regards

    Josh

  • If you are just looking for a filter, it is pretty easy:

    SELECT

    *

    FROM

    MyTable

    WHERE

    MyDateField >= DATEADD(DAY,-30,GETDATE())

    AND DATEPART(WEEKDAY,MyDateField) <> 1 --Not a sunday

    If you are trying to actually produce a list of dates, let me know and I will send you a recursive CTE query for this.

  • Here would be a CTE example for a list of actual day values.

    ; WITH DateList (DateValue, Level)

    AS

    (

    SELECT CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),101)), 0

    UNION ALL

    SELECT DATEADD(DAY,-1,DateValue), Level+1

    FROM DateList

    WHERE DATEADD(DAY,-1,DateValue) >= '1/1/2000'

    )

    SELECT

    DateValue

    FROM

    DateList

    WHERE

    DateValue >= DATEADD(DAY,-30,CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),101)))

    AND DATEPART(WEEKDAY,DateValue) <> 1

    OPTION (MAXRECURSION 30000)

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

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