問題描述
按存儲在文本列中的日期排序表 (Order table by date stored in text column)
我有應用程序並在 SQLite 上存儲了一些數據。我有一個名為 date 的列並存儲字符串數據格式,例如:
30.12.2018 ‑ some data 01.01.2019 ‑ some data 31.12.2018 ‑ some data
02.01.2019 ‑ 一些數據
但是當我想SELECT * FROM table ORDER BY date
時,這不能正常工作。我可以根據正確的日期順序訂購此表而不更改日期格式嗎?
例如,當我使用 SELECT * FROM mytable ORDER BY date
時,所需的結果:
02.01.2019 ‑ 一些數據 01.01.2019 ‑ 一些數據 31.12.2018 ‑ 一些數據 30.12.2018 ‑ 一些數據
參考解法
方法 1:
You must change the format of this column in order to avoid situations
where you need statements like this:
select * from mytable
order by
substr(date, 7, 4) || substr(date, 4, 2) || substr(date, 1, 2) || substr(date, instr(date, '‑')) desc
This part:
substr(date, 7, 4) || substr(date, 4, 2) || substr(date, 1, 2)
rearranges the date to YYYYMMDD
so it's comparable and can be sorted.
(by androidMan.、forpas)