Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select Table of Days For One Year SQLITE

I am trying to select a table of every date for the last year.

In SQL Server, I can run something like this:

SELECT TOP (DATEDIFF(DAY, DATEADD(YEAR, - 1, GETDATE()), GETDATE()) + 1) 
    Date = CAST(DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY a.object_id) - 1, DATEADD(YEAR, - 1, GETDATE())) AS DATE) 
FROM sys.all_objects a

It returns 1 column with 366 rows containing the dates from 1 year ago until now.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am looking for something equivalent in SQLITE.

>Solution :

You can do it with a recursive CTE:

WITH cte AS (
  SELECT DATE(CURRENT_DATE, '-1 year') date
  UNION ALL
  SELECT DATE(date, '+1 day') 
  FROM cte
  WHERE date < CURRENT_DATE
)
SELECT * FROM cte;

See the demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading