【问题标题】:How can I convert a datetime string to a UNIX timestamp in SQLite3?如何在 SQLite3 中将日期时间字符串转换为 UNIX 时间戳?
【发布时间】:2014-12-18 11:29:40
【问题描述】:

The documentation for SQLite3 datatypes

SQLite 的内置日期和时间函数能够将日期和时间存储为 TEXT、REAL 或 INTEGER 值

我想使用这些函数来存储INTEGER 值。我该怎么做? The documentation for SQLite3 datetime functions 描述了这些函数的参数类型,但没有说明返回类型。貌似返回类型是text

sqlite> select typeOf(datetime('2014-12-12 12:12:12.000'));
text

这不是我想要的——我想要一个整数值,将那个时间表示为 UNIX 时间戳。即使我创建了integer 类型的列并尝试在其中存储datetime(...) 值,SQLite 仍将其作为text 值存储在该integer 列中。

如何强制 datetime 函数和朋友返回 UNIX 时间戳,而不是 text 值?

【问题讨论】:

    标签: function datetime sqlite


    【解决方案1】:

    所有内置的日期/时间函数都返回字符串。

    要将字符串转换为数字,请使用CAST

    SELECT CAST(strftime('%s', 'now') AS INT);
    

    【讨论】:

    • 以前工作过:SELECT CAST(strftime('%s', strftime('%Y-%m-%dT00:00:00+00:00', <your_datetime_column>), 'unixepoch') as datetime) FROM <your_table>
    猜你喜欢
    • 2021-03-21
    • 2019-07-30
    • 2011-03-16
    • 1970-01-01
    • 2016-12-18
    • 2017-10-11
    • 1970-01-01
    • 2012-06-23
    • 2011-09-23
    相关资源
    最近更新 更多