Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate all dates between two dates

How can I retrieve all dates between '2015-10-02' to '2015-11-02' in SQLite? (String type) Result will be like:

'2015-10-03'
'2015-10-04'
'2015-10-05'
...
'2015-11-01'

This is not a question about SELECT * FROM myTable where myDate <= '2015-01-01' AND myDate >= '2015-01-31'. This is not about selecting all existing records which have a field between two days. I just want to retrieve all possible date values between two dates. I want to use them to query the count of record by days.

Date             Count
'2015-01-01'      19
'2015-01-02'      10
'2015-01-03'      0
...
like image 740
Andiana Avatar asked Oct 07 '15 01:10

Andiana


People also ask

How do I autofill dates between two dates in Excel?

Use the Fill Handle Select the cell that contains the first date. Drag the fill handle across the adjacent cells that you want to fill with sequential dates. at the lower-right corner of the cell, hold down, and drag to fill the rest of the series. Fill handles can be dragged up, down, or across a spreadsheet.

How do I generate days between two dates in SQL?

Use the DATEDIFF() function to retrieve the number of days between two dates in a MySQL database. This function takes two arguments: The end date. (In our example, it's the expiration_date column.)


1 Answers

This is not possible without a recursive common table expression, which was introduced in SQLite 3.8.3:

WITH RECURSIVE dates(date) AS (
  VALUES('2015-10-03')
  UNION ALL
  SELECT date(date, '+1 day')
  FROM dates
  WHERE date < '2015-11-01'
)
SELECT date FROM dates;
like image 63
CL. Avatar answered Oct 13 '22 23:10

CL.