从sATEite的DATETIME获取月份

时间:2009-03-16 13:47:03

标签: sql sqlite

我试图从SQLite中的DATETIME字段中提取月份。 month(dateField)strftime('%m', dateStart)的效果不同。

有什么想法吗?

9 个答案:

答案 0 :(得分:30)

我不明白,回答在你的问题中:

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

答案 1 :(得分:13)

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

如果您正在搜索月份名称,SQLite的核心发行版似乎不支持文本月份名称

答案 2 :(得分:12)

我猜你想把这个月作为一个字符串。不是最友好的外观,但你可能看到了重点。只需用变量替换日期('now')。

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 :(得分:9)

尝试使用以下内容:

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

答案 4 :(得分:1)

我猜strftime('%m', dateStart)对您不起作用,因为dateStart变量是日期/日期时间类型。

然后您必须使用:

strftime('%m', date(dateStart))

答案 5 :(得分:1)

这是一个解决所有情况的方法,无需使用when语句。希望它会有所帮助。

select substr('JanFebMarAprMayJunJulAugSepOctNovDec', 1 + 3*strftime('%m', date('now')), -3)

答案 6 :(得分:1)

这么多年过去了,我想我知道你的问题是什么了。 您可能将日期存储为字符串。 这样,当您从假设搜索数据时,您应该像这样编写 sql:

Select * from table where strftime('%m', datefield) = '05'

重要的是写“05”而不是“5”。使用“5”将使您获得零结果。

答案 7 :(得分:0)

这是对我有用的解决方案

MONTH_DICT={ "Jan" : '01', "Feb" : '02', "Mar" : '03', "Apr" : '04', "May" : '05', "Jun" : '06', "Jul" : '07', "Aug" : '08', "Sep" : '09', "Oct" : 10, "Nov" : 11, "Dec" : 12 }

self.cursor.execute("SELECT * FROM error_log WHERE strftime('%m',Date_column)=?",(MONTH_DICT[query_month],)) 

print('output:', self.cursor.fetchall())

答案 8 :(得分:-2)

SELECT strftime('%m',datetime_col) as "Month" FROM table_name;

只需将“datetime_col”替换为您的日期时间列