SQLite Order By Date1530019888000

前端 未结 9 701
臣服心动
臣服心动 2020-12-01 03:27

Every record in my SQLite database contains a field which contains a Date stored as a string in the format \'yyyy-MM-dd HH:mm:ss

相关标签:
9条回答
  • 2020-12-01 03:58

    You need to convert it to unix timestamp, and then compare them:

    SELECT * FROM data ORDER BY strftime('%s', date_column) DESC
    

    But this can be pretty slow, if there are lots of rows. Better approach would be to store unix timestamp by default, and create an index for that column.

    0 讨论(0)
  • 2020-12-01 03:58

    This will work for both date and time

    SELECT * 
    FROM Table 
    ORDER BY 
    julianday(dateColumn) 
    DESC Limit 1
    
    0 讨论(0)
  • 2020-12-01 04:05

    In my case everything works fine without casting column to type 'date'. Just by specifying column name with double quotes like that:

    SELECT * FROM 'Repair' ORDER BY "Date" DESC;
    

    I think SQLite makes casting by itself or something like that, but when I tried to 'cast' Date column by myself it's not worked. And there was no error messages.

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