How to make a query that returns data of the last 7 days without considering Sunday

6

I need some condition in SQL SERVER to return the result of the list of elements taking into account the last 7 days of creation of the same. However he can not consider Sunday as a valid day in the query.

Inquiry:

Select SearchId, getdate() as CreateDate from Security.Search
    
asked by anonymous 24.03.2015 / 19:25

1 answer

7

You did not give too many details, I think what you need is something like this:

SELECT SearchId, getdate() as CreateDate FROM Security.Search
        WHERE DATEDIFF(DAY, CreateDate, GETDATE()) < 8 AND DATEPART(DW, CreateDate) != 7

This will pick up the last 7 days but disregard the Sunday. If you want to take 7 days in total already disregarding Sunday in the account, then change the condition < 8 to <= 8 .

The first part of the condition takes the number of days you want ( DATEDIFF and GETDATE ) and the second part filters Sunday, which is considered to be day 7 of week obtained by DATEPART .

    
24.03.2015 / 20:06