SQL 在Sqlite中将日期转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3018018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a Date to a String in Sqlite
提问by Billy
Is there a way to convert a date to a string in Sqlite? For example I am trying to get the min date in Sqlite:
有没有办法在 Sqlite 中将日期转换为字符串?例如,我试图在 Sqlite 中获取最小日期:
SELECT MIN(StartDate) AS MinDate FROM TableName
I know in SQL Server I would use the SQL below to accomplish what I am trying to do:
我知道在 SQL Server 中我会使用下面的 SQL 来完成我想要做的事情:
SELECT CONVERT(VARCHAR(10), MIN(StartDate), 101) AS MinDate FROM TableName
Thanks!
谢谢!
回答by dan04
SQLite doesn't have a date type. What CURRENT_TIMESTAMP
actually returns is a string in the format YYYY-MM-DD HH:MM:SS
. The date/time formatting/calculation functionsaccept strings in this format, or alternatively, numbers, which are interpreted as Julian datesunless you use the 'unixepoch'modifier.
SQLite 没有日期类型。什么CURRENT_TIMESTAMP
实际返回的格式为一个字符串YYYY-MM-DD HH:MM:SS
。该日期/时间格式化/计算函数接受这种格式字符串,或者数字,这被解释为朱利安日期,除非你使用“unixepoch”修饰。
So, "converting a date to a string" depends on how you're storing your dates:
因此,“将日期转换为字符串”取决于您存储日期的方式:
- If you're storing them as Julian dates, use
DATE(YourDateColumn)
. - If you're storing them as Unix times, use
DATE(YourDateColumn, 'unixepoch')
. - If you're storing them as ISO 8601 strings, then, well you don't have to do anything. Unless you want a string in a different format, in which case you should use the
STRFTIME
function. - If you're storing them as some other string format, you really should consider using one of the supported formats so you can use the date/time functions.
- 如果您将它们存储为 Julian 日期,请使用
DATE(YourDateColumn)
. - 如果您将它们存储为 Unix 时间,请使用
DATE(YourDateColumn, 'unixepoch')
. - 如果您将它们存储为 ISO 8601 字符串,那么您无需执行任何操作。除非你想要一个不同格式的字符串,在这种情况下你应该使用该
STRFTIME
函数。 - 如果您将它们存储为其他字符串格式,您真的应该考虑使用一种受支持的格式,以便您可以使用日期/时间函数。
In all three date formats, chronological order is the same as lexicographical order, so the MIN
function works as expected.
在所有三种日期格式中,时间顺序与字典顺序相同,因此该MIN
函数按预期工作。
回答by Jimit Rupani
try this!!
尝试这个!!
SELECT * FROM Table WHERE strftime('%Y-%m-%d',CreatedAt) BETWEEN '2014-10-07' AND '2014-10-17'
回答by Ricardo Villamil
Try this:
尝试这个:
SELECT MIN(DATE('your-date-here')) as MinDate FROM TableName
And make sure your-date-here follows a well understood format by SQLite (See the Time String. section in the documentation)
并确保您的日期遵循 SQLite 易于理解的格式(请参阅文档中的Time String. 部分)