postgreSQL sorting with timestamps

Garamaru picture Garamaru · Jul 19, 2016 · Viewed 13.9k times · Source

I have the following SQL statement:

SELECT * FROM schema."table"
  WHERE "TimeStamp"::timestamp >= '2016-03-09 03:00:05'
  ORDER BY "TimeStamp"::date asc
  LIMIT 15

What do I expect it to do? Giving out 15 rows of the table, where the timestamp is the same and bigger than that date, in ascending order. But postgres sends the rows in the wrong order. The first item is on the last position. SQL Editor with result So has anyone an idea why the result is this strange?

Answer

klin picture klin · Jul 19, 2016

Use simply ORDER BY "TimeStamp" (without casting to date).