jav*_*rve 2 sql sqlite format date
我将Dates作为字符串存储在数据库中,格式为DD-MM-YYYY.
当我尝试在日期列上使用orderby进行选择查询时.我没有得到预期的结果.
结果的例子:28/02/2013 27/02/2013 01/03/2013
我的SQL查询:
SELECT * FROM data ORDER BY strftime('%s', date_column)
Run Code Online (Sandbox Code Playgroud)
谢谢.
问题是您将日期存储为DD-MM-YYYY字符串,这不仅会阻止日期自然排序为字符串,还会使用SQLite的日期和时间函数对其进行解析.单击链接并向下滚动到"时间字符串"部分.
SQLite期望自然顺序中的日期/时间字符串,最重要的数字到最不重要的数字,即YYYY-MM-DD.您可以使用字符串操作将DD-MM-YYYY字符串转换为该表单.例如:
select
substr(reversed_date, 7,4) || '-' ||
substr(reversed_date, 4, 2)|| '-' ||
substr(reversed_date, 1, 2) as proper_date
from (
select '12-03-2000' as reversed_date
)
;
Run Code Online (Sandbox Code Playgroud)
您可以将日期列转换为此格式(如@peterm建议的那样)或仅使用值proper_date进行排序.您不需要使用strftime它,但与日期相关的函数将使用此类值.
| 归档时间: |
|
| 查看次数: |
10039 次 |
| 最近记录: |