Let\'s pretend today is the 3rd of February.
And I have a table:
CREATE TABLE devotion
(
id serial NOT NULL,
date timestamp without time zone
}
If you were on MySQL i'd say just use RIGHT
, but the below should work on psql:
SELECT * FROM devotion ORDER BY substring(to_char( date, 'YYYY-MM-DD') from char_length(to_char( date, 'YYYY-MM-DD')) - 5)
Simpler:
ORDER BY (date1 < now()), date1
You can just order by the boolean value of the expression (date1 < now())
.
FALSE
sorts before TRUE
sorts before NULL
.
Here is another solution. Ofcourse above answer is good enough. You can't assume today is Feb 3rd since we are taking Now()
which stand for Feb 2nd
. So I used demo data 2013-02-01
. And this answer is mainly depending on your ID
. To say that ID is sequential so is the date. Anyone is free to comment on the dodgy part of this logic..
MYSQL DEMO
select id, `date`,
case when `date` > Date_Format(Now(),'%Y-%m-%d')
then id-3
else id+2 end as x
from demo
order by x asc
;
| ID | DATE | X |
--------------------------------------------
| 3 | March, 03 2013 00:00:00+0000 | 0 |
| 4 | April, 04 2013 00:00:00+0000 | 1 |
| 1 | January, 01 2013 00:00:00+0000 | 3 |
| 2 | February, 01 2013 00:00:00+0000 | 4 |
order by case when date1 > now() then 0 else 1 end case, date1
will give order of 3,4,1,2