Sqlite 将字符串转换为日期
我将日期作为字符串存储在 sqlite 数据库中,例如“28/11/2010”。 我想将字符串转换为日期。
具体来说,我必须在两个日期之间转换大量字符串日期。
在postgresql中,我使用to_date('30/11/2010','dd/MM/yyyy')
,我怎样才能用sqlite做同样的事情?
像这样的东西:
SELECT * FROM table
WHERE to_date(column,'dd/MM/yyyy')
BETWEEN to_date('01/11/2010','dd/MM/yyyy')
AND to_date('30/11/2010','dd/MM/yyyy')
I have date stored as string in an sqlite database like "28/11/2010".
I want to convert the string to date.
Specifically I have to convert lots of string dates between two dates.
In postgresql, I use to_date('30/11/2010','dd/MM/yyyy')
, how can I do the same thing with sqlite?
Something like this:
SELECT * FROM table
WHERE to_date(column,'dd/MM/yyyy')
BETWEEN to_date('01/11/2010','dd/MM/yyyy')
AND to_date('30/11/2010','dd/MM/yyyy')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
由于 SQLite 没有日期类型,您需要进行字符串比较才能实现此目的。为此,您需要反转顺序 - 例如从 dd/MM/yyyy 到 yyyyMMdd,使用类似
As SQLite doesn't have a date type you will need to do string comparison to achieve this. For that to work you need to reverse the order - eg from dd/MM/yyyy to yyyyMMdd, using something like
将日期保存为文本( 20/10/2013 03:26 )
进行查询并选择日期之间的记录?
更好的版本是:
20/10/2013 的子字符串给出 20131020
日期格式 DATE(20131021) - 使 SQL 能够处理日期并使用日期和时间函数。
或者
,这里是一行
Saved date as TEXT( 20/10/2013 03:26 )
To do query and to select records between dates?
Better version is:
the substr from 20/10/2013 gives 20131020
date format DATE(20131021) - that makes SQL working with dates and using date and time functions.
OR
and here is in one line
您应该研究的一件事是 SQLite 日期和时间函数,特别是当您需要进行大量操作时日期。这是使用日期的合理方法,但代价是更改内部格式(必须是 ISO,即 yyyy-MM-dd)。
One thing you should look into is the SQLite date and time functions, especially if you're going to have to manipulate a lot of dates. It's the sane way to use dates, at the cost of changing the internal format (has to be ISO, i.e. yyyy-MM-dd).
与脆弱的
substr
值相比,用户定义的函数 (UDF) 将是更好的方法。 UDF 可以编写为 Python 函数,然后 SQLite 将允许您在 SQL 查询中使用该函数,如下所示:以下是实现此功能的方法:
这将为您提供以下结果:
SQLite 与任何如此强大的东西等效的是缠结的编织您应该避免的
substr
和instr
调用。A user defined function (UDF) would be a better approach compared to brittle
substr
values. A UDF could be written as a Python function, and then SQLite will let you use that function within SQL queries like so:Here's how you could implement this:
This will give you these results:
The SQLite equivalent of anything this robust is a tangled weave of
substr
andinstr
calls that you should avoid.如果源日期格式不一致,则存在问题
使用
substr
函数,例如:1/1/2017 或 1/11/2017 或< /strong> 11/11/2017 或 1/1/17
所以
我使用临时表采用了不同的方法。此代码段输出“YYYY-MM-DD”+时间(如果存在)。
请注意,此版本接受日/月/年格式。如果你想要月/日/年
交换前两个变量
DayPart
和MonthPart
。此外,两年日期 '44-'99 假定为 1944-1999,而 '00-'43 假定为 2000-2043。If Source Date format isn't consistent there is some problem
with
substr
function, e.g.:1/1/2017 or 1/11/2017 or 11/11/2017 or 1/1/17
etc.
So I followed a different apporach using a temporary table. This snippet outputs 'YYYY-MM-DD' + time if exists.
Note that this version accepts Day/Month/Year format. If you want Month/Day/Year
swap the first two variables
DayPart
andMonthPart
. Also, two year dates '44-'99 assumes 1944-1999 whereas '00-'43 assumes 2000-2043.将
string
转换为date
小问题认为索引 mmm 3,3 但可以在日期字符串上添加一个月convert a
string
intodate
little issue think with indexing mmm 3,3 but works added a month on to the date string这是 fecha(TEXT) 格式日期 YYYY-MM-dd HH:mm:ss 例如我想要 Ene-05-2014 (2014-01-05) 的所有记录:
This is for fecha(TEXT) format date YYYY-MM-dd HH:mm:ss for instance I want all the records of Ene-05-2014 (2014-01-05):
我将日期存储为“DD-MON-YYYY”格式(2016 年 6 月 10 日),下面的查询可供我搜索两个日期之间的记录。
I am storing the date as 'DD-MON-YYYY format (10-Jun-2016) and below query works for me to search records between 2 dates.
我有一个数据库,其中日期以 d F Y 格式存储(2017 年 11 月 20 日),并将其转换为机器可读的日期(Ymd),我使用此方法将整个表更新为正确的格式。
如果您只想要日期格式,请查看内部选择我如何格式化日期。
I have a database where dates are stored in
d F Y
format (20 Nov 2017) and to convert it to a machine readable date(Y-m-d) I use this method to update the entire table to a proper format.If you only want the date formatting look at the inner select how I formatted the date.