Sqlite-更改日期格式
有没有办法在Sqlite中更改此格式以使用selectSqlite-更改日期格式,sql,sqlite,date,sql-update,Sql,Sqlite,Date,Sql Update,有没有办法在Sqlite中更改此格式以使用select 是否转换为正常的日期格式,例如(yyyy-mm-dd)?SQLite不支持将月份名称或缩写转换为数字的任何功能。 这可以通过执行映射的CTE完成,然后连接到表: WITH cte(month, month_name) AS (VALUES ('01', 'JAN'), ('02', 'FEB'), ('03', 'MAR'), ('04', 'APR'), ('05', 'MAY'), ('06', 'JUN'), ('07',
是否转换为正常的日期格式,例如(yyyy-mm-dd)?SQLite不支持将月份名称或缩写转换为数字的任何功能。
这可以通过执行映射的CTE完成,然后连接到表:
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;
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))
如果要更新表,请执行以下操作:
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;
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))
将列的名称替换为列的名称。请参阅。
是完整月份还是三个字母的月份缩写?例如,是2021年1月10日还是2021年1月10日?3个字母的月份缩写