How to get Previous Month number and Month before Previous Month number in SQLite,
I am using strftime('%m','now') for getting current month number but not finding anything to extract previous month number, how to do this
CodePudding user response:
You can use the date function to get the previous month:
SELECT strftime('%m', date('now','start of month','-1 month'));
SELECT strftime('%m', date('now','start of month','-2 month'));
It's fairly self explanatory: This uses the date function to get the first day of the current month (to prevent issues on the last days of a month with more days than the previous month), then subtract 1 or 2 months. Then use strftime to pull out just the month number.
CodePudding user response:
SQLite has a modulo operator (%), so if month is the current month number, you can calculate:
previous_month = (month - 2) % 12 1
month_before_previous = (month - 3) % 12 1
