SQLITE DB sort query by string formatted date field

前端 未结 1 847
别跟我提以往
别跟我提以往 2021-01-22 00:21

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 trie

相关标签:
1条回答
  • 2021-01-22 00:41

    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.

    0 讨论(0)
提交回复
热议问题