SQLite 查询问题
2个问题:
首先,在MSSQL中,我可以通过执行以下操作将nvarchar转换为datetime
cast('5/31/2011 12:00:00 AM' as datetime) as convertedtodate
Result: 2011-05-31 00:00:00.000
我如何在SQLite中做到这一点?
其次,MS SQL 的 datediff
函数在 sqlite 上的等效项是什么?例如:
datediff(Day,'5/30/2011 12:00:00 AM','5/31/2011 12:00:00 AM') as DateAge
结果:1
2 questions:
First, in MSSQL, I can convert nvarchar to datetime by doing
cast('5/31/2011 12:00:00 AM' as datetime) as convertedtodate
Result: 2011-05-31 00:00:00.000
How do I do it in SQLite?
Second, what is the equivalent of MS SQL's datediff
function on sqlite? E.g.:
datediff(Day,'5/30/2011 12:00:00 AM','5/31/2011 12:00:00 AM') as DateAge
Result: 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要回答您的第一个问题(将字符串转换为日期),答案是“这取决于”。由于 SQLite 没有特定的日期字段,因此您可能不需要转换它。您可以将其存储在字符串字段中(选项为 string、real 或 int,用于日期存储)。如果您想将字符串转换为 int (这将是自 1970-01-01 以来的秒数),您可以使用
strftime
方法,如下所示:至于问题的第二部分(两个日期之间的差异),您可以在 SQLite 中使用以下代码:
这将为您提供两个日期之间的秒数。您可以尝试使用这些信息以及更多信息,以准确地从 SQLite 中获取您想要的信息。以下是一些可以帮助您的资源:
http://www.sqlite.org/cvstrac/wiki?p =DateAndTimeFunctions
http://www.sqlite.org/datatype3.html
To answer your first question (convert string to date), the answer is "it depends". Since SQLite does not have a specific date field, you may not need to convert it. You could just store it in a string field (the options are sting, real, or int for date storage). If you want to convert the string to an int (which would be the number of seconds since 1970-01-01), you would use the
strftime
method like so:As for the second part of your question (difference between two dates), you would use the following code in SQLite:
That will give you the number of seconds between the two dates. You can play around with this information, as well as a lot more, to get exactly what you are looking for from SQLite. Here are a couple resources that will help you out:
http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions
http://www.sqlite.org/datatype3.html