SQLite Order By Date1530019888000

前端 未结 9 700
臣服心动
臣服心动 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:46

    When you sure the format of text field is yyyy-MM-dd HH:mm:ss (ex.: 2017-01-02 16:02:55), So It works for me simply:

    SELECT * FROM Table ORDER BY dateColumn DESC Limit 1

    Without any extra date function!

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

    You can convert your column sent_date_time to yyyy-MM-dd format and then order by date:

    1) substr(sent_date_time,7,4)||"-"||substr(sent_date_time,1,2)||"-"||substr(sent_date_time,4,2) as date
    2) order by date desc
    
    0 讨论(0)
  • 2020-12-01 03:47

    you can do it like this

    SELECT * FROM Table ORDER BY date(dateColumn) DESC Limit 1
    
    0 讨论(0)
  • 2020-12-01 03:47

    You can also use the following query

    "SELECT * FROM Table ORDER BY strftime('%Y-%m-%d %H:%M:%S'," + dateColumn + ") DESC  Limit 1"
    
    0 讨论(0)
  • 2020-12-01 03:47

    If you do a lot of date sorting/comparison, you may get better results by storing time as ticks rather than strings, here is showing how to get 'now' in ticks with:

    ((strftime('%s', 'now') - strftime('%S', 'now') + strftime('%f', 'now')) * 1000)
    

    (see https://stackoverflow.com/a/20478329/460084)

    Then it's easy to sort, compare, etc ...

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

    For me I had my query this way to solve my problem

    select *  from Table order  by datetime(datetimeColumn) DESC LIMIT 1
    

    Since I was storing it as datetime not date column

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