如何将日期时间字符串转换为SQLite3中的UNIX时间戳? [英] How can I convert a datetime string to a UNIX timestamp in SQLite3?

查看:333
本文介绍了如何将日期时间字符串转换为SQLite3中的UNIX时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQLite3数据类型的文档说:

The documentation for SQLite3 datatypes says


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

我想使用这些函数来存储 INTEGER 值。我该怎么做? SQLite3日期时间函数的文档描述了这些函数的参数类型,但没有提及返回类型。看起来返回类型是 text

I want to use those functions to store INTEGER values. How do I do so? The documentation for SQLite3 datetime functions describes the types of the arguments to those functions, but says nothing of the return type. It seems that the return type is text:

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

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

This is not what I want -- I want an integer value representing that time as a UNIX timestamp. Even when I create a column of type integer and attempt to store a datetime(...) value in it, SQLite stores it as a text value in that integer column.

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

How do I force the datetime function and friends to return a UNIX timestamp, instead of a text value?

推荐答案

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

To convert a string into a number, use CAST:

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

这篇关于如何将日期时间字符串转换为SQLite3中的UNIX时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆