sql—将时间字符串转换为适当的格式,并在sqllite中对其执行算术运算

gkl3eglg  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(222)

我在sqlite数据库中有数据,其中时间戳是文本,格式为10:15:28 pm和9:43:43 pm。
如何将它们转换为时间戳?

py49o6xq

py49o6xq1#

在sqlite中,需要使用字符串函数将值转换为数据库可以理解为 time . 对于这些上午/下午日期,一个选项是将前8个字符转换为 time ,并向以结尾的值添加12小时 'PM' .

time(
    substr(mycol, 1, 8), 
    '+' || case when mycol like '%PM' then '12' else '0' end || 'hour'
)

从那时起,您可以使用日期函数。说你想知道时间的不同 mycol1 以及 mycol2 几秒钟后:

strftime(
    '%s', 
    time(
        substr(mycol1, 1, 8), 
        '+' || case when mycol1 like '%PM' then '12' else '0' end || 'hour'
    )
) - strftime(
    '%s', 
    time(
        substr(mycol1, 1, 8), 
        '+' || case when mycol1 like '%PM' then '12' else '0' end || 'hour'
    )
)

相关问题