SQLite 查询问题

发布于 2024-11-06 06:20:41 字数 397 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-11-13 06:20:41

要回答您的第一个问题(将字符串转换为日期),答案是“这取决于”。由于 SQLite 没有特定的日期字段,因此您可能不需要转换它。您可以将其存储在字符串字段中(选项为 string、real 或 int,用于日期存储)。如果您想将字符串转换为 int (这将是自 1970-01-01 以来的秒数),您可以使用 strftime 方法,如下所示:

strftime('%s','2011-05-12 01:03:00')

至于问题的第二部分(两个日期之间的差异),您可以在 SQLite 中使用以下代码:

strftime('%s','2011-05-12 01:03:00') - strftime('%s','2011-05-08 11:54:09')

这将为您提供两个日期之间的秒数。您可以尝试使用这些信息以及更多信息,以准确地从 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:

strftime('%s','2011-05-12 01:03:00')

As for the second part of your question (difference between two dates), you would use the following code in SQLite:

strftime('%s','2011-05-12 01:03:00') - strftime('%s','2011-05-08 11:54:09')

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文