Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite sorting by date column

I am storing date in ISO8601 format example 2015-04-15T10:54:14Z in sqlite table, I want youngest date from table. below are the dates in my sqlite table

2015-04-15T10:54:14Z 
2015-04-15T10:54:115Z
2015-04-15T10:54:216Z
2015-04-15T10:54:320Z
2015-04-15T10:54:422Z

I am trying below query:

SELECT * FROM Table1 ORDER BY datetime("date_column") DESC ;

but I am not getting appropriate result.

like image 990
Anwar Shaikh Avatar asked Oct 24 '25 05:10

Anwar Shaikh


2 Answers

ISO 8601 datetime stamps normalized to UTC have the nice property that the alphabetical (lexicographic) order is also temporal order.

You don't need the datetime(), you can just ORDER BY date_column DESC to sort them newest first, and you can add LIMIT 1 to get just the newest one.

like image 122
laalto Avatar answered Oct 26 '25 19:10

laalto


Update your query to this:

SELECT * FROM Table1 ORDER BY date_column DESC LIMIT 1
like image 41
Bernardmoes Avatar answered Oct 26 '25 18:10

Bernardmoes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!