S_S
S_S

Reputation: 1402

SQL Query to get dates of X days after current date

How can i construct a query that would return some 'x' number of dates after the current date (sysdate)?

For example if today is 18-May-2018 and I ask for x=2, then I should get

19-May-2018

20-May-2018

as the output.

Upvotes: 2

Views: 910

Answers (3)

pravinsham
pravinsham

Reputation: 31

since you have written sysdate, I believe you are looking for an oracle database, if you are looking for an oracle database please use the following statement

select  trunc(sysdate)+level ldate from dual
connect by level <&x;

above query will ask for x, when you give the value of x as 3, it will generate two days from sysdate

Upvotes: 1

Kaushik Nayak
Kaushik Nayak

Reputation: 31676

You may use generate_series.

SELECT generate_series(current_date + 1, 
                       current_date + 2, 
                       interval '1' day)

Docs

In plsql, you may set x as variable and then use it in your query.

knayak=# \set x 5
knayak=# SELECT generate_series(current_date + 1,                                                                                                            
                                current_date + :x,                                                                                                                        
                                interval '1' day);

 generate_series      
---------------------------
 2018-05-19 00:00:00+05:30
 2018-05-20 00:00:00+05:30
 2018-05-21 00:00:00+05:30
 2018-05-22 00:00:00+05:30
 2018-05-23 00:00:00+05:30

You may format the dates in desired format with TO_CHAR

SELECT  to_char(dt,'DD-MON-YYYY') as dates
FROM    generate_series(current_date + 1, 
                        current_date + 2, 
                        interval '1' day) as dt

dates    
-------------
19-MAY-2018
20-MAY-2018

Upvotes: 1

Pham X. Bach
Pham X. Bach

Reputation: 5442

If you're using PostgreSQL, you could use this:

SELECT current_date + s.a AS dates 
FROM generate_series(1, 2, 1) AS s(a);

And change 2 to value of your x

Upvotes: 1

Related Questions