Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Date between today and 7 days after

Tags:

sql-server

I need to write a view where I need to select all the elements where the date is between today and + 7 days.

I have for example:

2018-11-30
2018-06-30
2018-10-31
2018-05-31
2018-04-16
2018-04-12
2018-04-02

From this list I need only these two elements:

2018-04-16

2018-04-12

I have tried already like this:

 WHERE sl.GettingBackDate > CAST(DATEADD(DAY, -7, GETDATE()) as DATE)

but this returns all the elements where the date is greater than today

WHERE sl.GettingBackDate BETWEEN DATEADD(DAY,-7,GETDATE()) AND GETDATE()

this don't return any element

I created an SQLFiddle - http://sqlfiddle.com/#!18/27d5c/2

Can you advise?

like image 444
Kar Avatar asked Apr 12 '18 09:04

Kar


2 Answers

You can add the following where condition

WHERE 
    sl.GettingBackDate >= CONVERT(DATE, GETDATE()) AND 
    sl.GettingBackDate <= CONVERT(DATE,DATEADD(DAY,7,GETDATE()))
like image 169
Emdad Avatar answered Nov 04 '22 01:11

Emdad


Use this double filter.

WHERE 
    sl.GettingBackDate >= CONVERT(DATE, GETDATE()) AND -- From today onwards
    sl.GettingBackDate < CONVERT(DATE, GETDATE() + 8) -- Less than 8 days from now (strict)
like image 39
EzLo Avatar answered Nov 04 '22 00:11

EzLo