Skip to content

Instantly share code, notes, and snippets.

@palmerj
Last active January 28, 2018 00:32
Show Gist options
  • Save palmerj/0c6948dd9590405269d88f340fdb5489 to your computer and use it in GitHub Desktop.
Save palmerj/0c6948dd9590405269d88f340fdb5489 to your computer and use it in GitHub Desktop.
SQLite3 - Generate date time ranges using SQL recursive query
DROP TABLE IF EXISTS date_ranges;
CREATE TEMP TABLE date_ranges (start_datetime DATETIME);
WITH RECURSIVE
date_ranges(start_datetime) AS (
VALUES('2011-05-01 00:00:00')
UNION ALL
SELECT datetime(start_datetime, '+1 month')
FROM date_ranges WHERE start_datetime < datetime('now', '-1 month')
)
SELECT start_datetime, datetime(start_datetime, '+1 month') AS end_datetime
FROM date_ranges;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment