如何从sqlite数据库中的日期中仅提取月份?

问题描述 投票:0回答:2

我想获取特定月份发生的一些数据。例如,我需要选择 7 月份加入的所有员工的姓名(无论日期)。从数据库的日期字段中单独选择特定月份字段的查询是什么?如何比较数据库中的月份字段(以 mm/dd/yyyy 格式存储的日期)和用户给定的月份值。我正在使用 sqlite3 数据库,日期字段设置为文本。 预先感谢。

sqlite date
2个回答
2
投票

SQLite 仅具有 一小组日期和时间函数。你可以像这样使用它们:

sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1 (f1 string);
INSERT INTO "t1" VALUES('03/31/1970');
COMMIT;

sqlite> select substr(f1, 7) || '-' || substr(f1, 0, 3) || '-' || substr(f1, 4, 2) from t1;
1970-03-31

sqlite> select strftime("%m", substr(f1, 7) || '-' || substr(f1, 0, 3) || '-' || substr(f1, 4, 2)) from t1;
03

0
投票

我在 SQLITE 中使用此代码解决了这个问题。

如果您有这样的格式的数据:“2021-12-23 12:33:01” 然后将其转换为 strftime() 格式函数。

ex.-> Select strftime('%d/%m/%Y',EntryDate) as Date  from table_name;

然后输出进来

     "2021-12-23 12:33:01"  to "23-12-2021"

然后触发此查询以从 SQLITE 中的 MONTH NUMBER 获取 MONTH NAME

-> using case we can fetch it.


 Select strftime('%d/%m/%Y',checkInDate) as Date, 
  case strftime('%m', checkInDate)  when '01' then 'JAN' 
  when '02' then 'FEB' when '03' then 'MAR' when '04' then 'APR' when '05' then 'MAY' when '06' then 'JUN' 
  when '07' then 'JUL' when '08' then 'AUG' when '09' then 'SEP' when '10' then 'OCT'  
  when '11' then 'NOV' when '12' then 'DEC' else '' end as Month   from AttendanceTable

☻♥ 完成保留代码。

© www.soinside.com 2019 - 2024. All rights reserved.