如何在sqlite中使用sql将字符串转换为日期时间

Net*_*205 -1 sql sqlite datetime

如何转换string为in的datetime使用?sqlsqlite

在我的SQLite数据库中,puttimenvarchar类型和存储数据为空,空字符串,2013年10月23日,2013年10月23日十三时30分25秒,2013年10月24日9时30分22秒

我使用下面的查询进行转换,但是'2013-10-24 9:30:22'无法转换成功,该怎么做:

select puttime, datetime(puttime) from tb_news
Run Code Online (Sandbox Code Playgroud)

结果:

PutTime datetime(puttime)
2013-05-06          2013-05-06 00:00:00
2013-10-23          2013-10-23 00:00:00
2013-10-23 13:30:25 2013-10-23 13:30:25
2013-10-23 18:00:00 2013-10-23 18:00:00
2013-10-24 17:32:33 2013-10-24 17:32:33
2013-10-24 22:49:43 2013-10-24 22:49:43
2013-10-24 9:30:22  
2013-10-25 00:01:33 2013-10-25 00:01:33
Run Code Online (Sandbox Code Playgroud)

谢谢。

LS_*_*ᴅᴇᴠ 5

SQLite期望的最接近日期/时间格式为YYYY-MM-DD HH:MM:SS

查看您的数据,可以看出只有偏差为YYYY-MM-DD H:MM:SS

因此,让我们0在需要时添加一个:

SELECT puttime, DATETIME(
    CASE SUBSTR(puttime, 14, 1) WHEN ':' THEN puttime -- Found ':' after HH
    ELSE SUBSTR(puttime, 1, 11)||'0'||SUBSTR(puttime, 12) END
) FROM tb_news
Run Code Online (Sandbox Code Playgroud)