0

I need to convert this query from MySQL format to SQLite. I'm trying myself but I've found some difficulty.

In SQLite, the curdate() and the interval functions do not exist.

select a.Date 
from (
    select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between '2010-01-20' and '2010-01-24' 

2 Answers 2

2

What this query actually does is just generating lots of consecutive dates (up to one thousand previous days).

In SQLite 3.8.3 or later, this can be done more easily with a recursive common table expression:

WITH RECURSIVE dates(d)
AS (VALUES('2010-01-20')
    UNION ALL
    SELECT date(d, '+1 day')
    FROM dates
    WHERE d < '2010-01-24')
SELECT d AS date FROM dates;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man!! you're awesome!! :) I've checked the previous answer because he has answered my question but I think that your solution is better than my and I will take for my project! :)
1

Here is the basic syntax:

select a.Date 
from (select date('now', '-'||(a.a + (10 * b.a) + (100 * c.a))|| ' days') as Date
      from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
           ) a cross join
           (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
           ) b cross join
           (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
           ) c
    ) a;

I left out the where clause because those dates are more than 1000 days in the past, so they won't select anything anyway.

1 Comment

Thanks!! I've checked your answer but I've decided to substitute the query with the query on the next answer because is better for me. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.