【问题标题】:JulianDay() and Strftime() returning null when calculating difference between two datesJulianDay() 和 Strftime() 在计算两个日期之间的差异时返回 null
【发布时间】:2015-09-15 22:38:16
【问题描述】:

我在从 SQLite 数据库中的表中检索两个日期列之间的差异时遇到问题。当我使用 CocoaMySQL MesaSQLite 3.0.9 执行查询时,结果是“NULL”。我有这些列的值的记录。 我的查询是

选择 cast ( ( strftime('%s',t.timestamp2)-strftime('%s',t.timestamp1 ) ) 真实 )/60/60 AS 已过 FROM 时间戳 AS t; SELECT (STRFTIME('%s',timestamp1) - STRFTIME('%s',timestamp2)) / -3600 from timestamps where objid= 3939

结果:空

从 objid = 3939 的时间戳中选择 timestamp1,timestamp2。

结果:2014-01-29 02:55:00 AM 2014-01-29 03:00:00 AM(意味着我在时间戳表中有数据)

选择 julianday('now') - julianday('1776-07-04');

结果:87287.5314

从 objid =3939 的时间戳中选择 julianday(timestamp1) - julianday(timestamp2)

结果:空值

'timestamp1' & 'timestamp2' 是 'timestamps' 表中的列。它们有带值的记录。

请提供解决方案以尽快解决此问题。 我无法添加屏幕截图。

【问题讨论】:

    标签: sqlite strftime


    【解决方案1】:

    2014-01-29 02:55:00 AM 等日期戳不在format understood by SQLite 中。仅支持 24 小时制,您需要删除 AM/PM。

    除此之外,如果您打算在日期时间上进行数学运算,请首先考虑将它们存储在使计算变得简单、快速和可靠的格式中,例如 unixepoch 秒。

    【讨论】:

    • 感谢您的回复。我以这种方式完成 select timestamp1, timestamp2,cast( (strftime('%s',trim(ts.timestamp2,'AM/PM'))-strftime('%s',trim(ts.timestamp1,'AM/ PM'))) AS real)/60/60 来自 objid = 3939 的时间戳。这对我有用。
    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 2011-08-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多