SQLITE DB sort query by string formatted date field

我是研究僧i 提交于 2019-12-02 03:06:46

问题


I have a SQLITE DB with a string field which contains a date in the following format: "01.01.2012". What I want is to sort the by this string formatted date in a query. I tried this without success:

SELECT * FROM fahrer, fahrzeuge, nutzungsarten, fahrtenbuch WHERE fahrtenbuch.nutzungsart_id = nutzungsarten._id AND fahrtenbuch.fahrzeuge_id = fahrzeuge._id AND fahrtenbuch.fahrer_id = fahrer._id ORDER BY strftime ('%d.%m.%Y', fahrtenbuch.startdatum)

What I am doing wrong?


回答1:


The values in the startdatum column are not in a format that SQLite recognizes, so you cannot use it as a parameter to strftime. (Even if it worked, the result would not be sorted correctly because that date format does not begin with the most significant field, the year.)

You could try to extract the date fields so that the sorting is equivalent with the yyyy-mm-dd order:

SELECT ...
ORDER BY substr(startdatum, 7, 4),
         substr(startdatum, 4, 2),
         substr(startdatum, 1, 2)

But I would recommend to convert the values into a supported format in the first place.



来源:https://stackoverflow.com/questions/13166445/sqlite-db-sort-query-by-string-formatted-date-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!