Get month from DATETIME in sqlite

2019-01-12 02:23发布

问题:

I am trying to get extract the month from a DATETIME field in SQLite. month(dateField) does not work as well as strftime('%m', dateStart).

Any ideas?

回答1:

I don't understand, the response is in your question :

select strftime('%m', dateField) as Month ...


回答2:

I'm guessing you want to get the month as a string. Not the most friendly looking but you probably see the point. Just replace date('now') with your variable.

select case strftime('%m', date('now')) when '01' then 'January' when '02' then 'Febuary' when '03' then 'March' when '04' then 'April' when '05' then 'May' when '06' then 'June' when '07' then 'July' when '08' then 'August' when '09' then 'September' when '10' then 'October' when '11' then 'November' when '12' then 'December' else '' end
as month 


回答3:

SELECT strftime('%m', datefield) FROM table 

If you are searching the month name, text month names does not seems to be supported by the core distribution of SQLite



回答4:

Try using the following:

select strftime('%m', datetime(datefield, 'unixepoch')) as month from table