Sqlite - 更改日期格式

Sqlite - change date format

有什么方法可以在 Sqlite 中更改此格式以使用 select

为正常日期格式,例如(yyyy-mm-dd) ??

SQLite 不支持任何可以将月份名称或缩写转换为数字的函数。
这可以通过执行映射的 CTE 完成,然后连接到 table:

WITH cte(month, month_name) AS (VALUES
  ('01', 'JAN'), ('02', 'FEB'), ('03', 'MAR'), ('04', 'APR'), ('05', 'MAY'), ('06', 'JUN'), 
  ('07', 'JUL'), ('08', 'AUG'), ('09', 'SEP'), ('10', 'OCT'), ('11', 'NOV'), ('12', 'DEC') 
)
SELECT SUBSTR(t.col, 1, 4) || '-' || c.month || '-' || printf('%02d', SUBSTR(t.col, 8)) date
FROM tablename t INNER JOIN cte c
ON t.col LIKE '%' || c.month_name || '%'
ORDER BY date;

如果要更新 table:

WITH cte(month, month_name) AS (VALUES
  ('01', 'JAN'), ('02', 'FEB'), ('03', 'MAR'), ('04', 'APR'), ('05', 'MAY'), ('06', 'JUN'), 
  ('07', 'JUL'), ('08', 'AUG'), ('09', 'SEP'), ('10', 'OCT'), ('11', 'NOV'), ('12', 'DEC') 
)
UPDATE tablename AS t
SET col = SUBSTR(t.col, 1, 4) || '-' || 
          (SELECT c.month FROM cte c WHERE t.col LIKE '%' || c.month_name || '%') || '-' || 
          printf('%02d', SUBSTR(t.col, 8))

col 替换为列名。

参见demo