0

I have a sqlite database in my project and I have 3 year,month and day fields. I want to get last week or last month information from my table and show it in a listView using cursor adapter. I don't know how to do that. my table is not very big also my records are sorted by date so iteration can be an answer. please help me in any way.

2 Answers 2

0
SELECT * FROM myTable WHERE DATE(timeStamp) >= DATE('now', 'weekday 0', '-7 days');
1
  • please explain more about this syntax. I want to use my fields to retrieve date. see my edit Aug 6, 2013 at 12:32
0

if the table is like this

CREATE TABLE t(some_data, year, month, day);

INSERT INTO t(some_data, year, month, day)
                    SELECT ABS(RANDOM()%10000), 2013,  8,  6 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  8,  4 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  8, 01 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013, 07, 31 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 30 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 25 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013, 07, 24 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 23 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 20 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7, 10 UNION ALL
                    SELECT ABS(RANDOM()%10000), 2013,  7,  9
;

this is a possible solution

SELECT some_data, year
    || '-' ||
    CASE WHEN LENGTH(month) = 2 THEN month
         WHEN LENGTH(month) = 1 THEN 0 || month
    END
    || '-' ||
    CASE WHEN LENGTH(day) = 2 THEN day
         WHEN LENGTH(day) = 1 THEN 0 || day
    END as d
FROM t
WHERE DATE(d) >= DATE('now', '-7 days')
ORDER BY d;


some_data   d         
----------  ----------
7455        2013-07-30
6123        2013-07-31
6903        2013-08-01
9332        2013-08-04
1895        2013-08-06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.