Get records from a specific month using SQLite
Question
How would I write a SQLite query to select all records from a specific month? My dates are stored as Unix timestamps.
PHP code is allowed in your solution if it's required. SQLite2-compatible queries only, please. :)
Solution
If you want an efficient query then you should use BETWEEN:
SELECT *
FROM Table1
WHERE date BETWEEN $start AND $end
Where start
and end
are the unix timestamps for the start of the month and the end of the month. These can be calculated in PHP using mktime and sent as parameters.
OTHER TIPS
Use:
SELECT *
FROM TABLE
WHERE DATETIME(your_unix_timestamp_col, 'unixepoch') BETWEEN YYYY-MM-DD
AND YYYY-MM-DD
Replace the YYYY-MM-DD
with the dates you desire. See the reference page for other supported formats.
Reference:
Without calculating last day in month
SELECT *
FROM table
WHERE strftime('%Y-%m',date(unix_timestamp,'unixepoch','localtime')) = '2010-03'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow