排序日期存储在SQLite数据库中 [英] Sort Date stored in SQLite database

查看:281
本文介绍了排序日期存储在SQLite数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQLite数据库(而不是我的),我必须按照日期排序数据。



我在网络上做了很多搜索,找不到解决方案!

带有日期的字段类型为Date,格式为: DD / MM / YYYY HH:mm:SS



我尝试过很多功能,如strftime,datetime,date,...但是我无法正确排序日期。
例如,如果我这样做:

  SELECT * FROM events ORDER BY start_date 

日期04/10/2013 00:00:00是在03/12/2013之后...你知道我是什么意思 ?



我真的不知道我可以用这种格式排序。



感谢您的帮助

p>

更新1:



我尝试过这样的东西:

  SELECT start_date FROM events ORDER BY strftime('%d /%m /%Y%H:%M:%S',start_date)

这是正确的做法吗?
结果:没有错误,但没有改变。日期不排序。



更新2:



laalto解决方案:

  select * from tab order by substr(col,7,4)||' - '|| substr(col,4 ,2)||' - '|| substr(col,1,2)||'T'|| substr(col,12); 


解决方案

最好的方法是使数据在插入时排序阶段例如通过插入ISO-8601邮票或unixtime整数,但由于它不是您的数据库,我们假设您不能影响。



您可以使用 substr()从datetime字符串中提取部分并构造eg一个ISO-8601日期时间字符串,您可以按字典顺序排序,从而按时间顺序排列。



示例:

  sqlite>创建表选项卡(col); 
sqlite>插入选项卡值('04 / 10/2013 00:00:00'),('03/12/2013 00:00:00');
sqlite>选择substr(col,7,4)||' - '|| substr(col,4,2)||' - '|| substr(col,1,2)||'T'|| substr(col, 12)按日期从标签顺序开始;
2013-10-04T00:00:00
2013-12-03T00:00:00
sqlite> select * from tab order by substr(col,7,4)||' - '|| substr(col,4,2)||' - '|| substr(col,1,2)||'T' | substr(col,12);
04/10/2013 00:00:00
03/12/2013 00:00:00


I'm using a SQLite database (not mine) and I have to sort some data, order by date.

I have done many searches on the web but no solutions found!
The type of the field with the dates is "Date", with the format: DD/MM/YYYY HH:mm:SS

I tried many functions like strftime, datetime, date, ... But I can't sort the date correctly. For example, if I do something like this:

SELECT * FROM events ORDER BY start_date

The date 04/10/2013 00:00:00 is after 03/12/2013... You know what I mean ?

I really don't know how I can sort in this format.

Thanks for your help

UPDATE 1:

I tried something like this:

SELECT start_date FROM events ORDER BY strftime('%d/%m/%Y %H:%M:%S', start_date)

Is this the right way to do ? Results: No error, but nothing has changed. The dates are not sorted.

UPDATE 2:

Solution by laalto:

select * from tab order by substr(col,7,4)||'-'||substr(col,4,2)||'-'||substr(col,1,2)||'T'||substr(col,12);

解决方案

The best way is to make the data sortable at insertion phase e.g. by inserting ISO-8601 stamps or unixtime integers, but since it's not your database, let's assume you can't influence that.

You can use substr() to extract parts from the datetime string and construct e.g. a ISO-8601 datetime string that you can sort lexicographically, resulting in chronological order.

Example:

sqlite> create table tab(col);
sqlite> insert into tab values('04/10/2013 00:00:00'),('03/12/2013 00:00:00');
sqlite> select substr(col,7,4)||'-'||substr(col,4,2)||'-'||substr(col,1,2)||'T'||substr(col,12) as date from tab order by date;
2013-10-04T00:00:00
2013-12-03T00:00:00
sqlite> select * from tab order by substr(col,7,4)||'-'||substr(col,4,2)||'-'||substr(col,1,2)||'T'||substr(col,12);
04/10/2013 00:00:00
03/12/2013 00:00:00

这篇关于排序日期存储在SQLite数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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