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

Asked

Viewed 474 times

6

I need some condition in SQL SERVER return the result of the list of elements taking into account the last 7 days of creation of the same. But he cannot consider Sunday as a valid day in the consultation.

Consultation:

Select SearchId, getdate() as CreateDate from Security.Search

1 answer

7


You didn’t 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

I put in the Github for future reference.

This will take the last seven days but disregard Sunday. If you want to take 7 days in total already disregarding Sunday on account, then change the condition < 8 for <= 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 the 7th day of the week obtained by the function DATEPART.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.