Mr R
Mr R

Reputation: 95

sql query with date intevals

Hi I need help to create a query that return a result based on date interval but I can't get it to work correctly.

I would like to achieve a result giving me the records with a date that are within a historic time span:

day -1 to -7   */from yesterday and -7 days */      
day -8 to -14  */the date is between -8 and -14 days from today      

For the first interval I use this where clause:

...
where `invoiceExpDate` >= date_add(now(), INTERVAL - 7 DAY)

how can I modify this to NOT give me the records for today??

For the second interval I use:

...
where datediff(invoiceExpDate,now())<= 14 
AND datediff(invoiceExpDate,now())> 7 
AND  `invoiceExpDate` > now()

I can't get them to work. CAn you help me with the correct where clause to return what I want?

Thanks

Upvotes: 0

Views: 125

Answers (2)

Wael Mansour
Wael Mansour

Reputation: 51

LogDate BETWEEN DATEADD(dd, -7, GETDATE()) AND DATEADD(dd, -1, GETDATE())

Upvotes: 0

Bort
Bort

Reputation: 7618

I think you can combine date_add() and BETWEEN

For your first clause

...
WHERE `invoiceExpDate` BETWEEN 
    date_add(now(), INTERVAL - 7 DAY) AND 
    date_add(now(), INTERVAL - 1 DAY)

Similar pattern for the second.

Upvotes: 1

Related Questions