Re: Making NULL entries appear first when ORDER BY <field>

From: Ragnar Hafstað <gnari(at)simnet(dot)is>
To: Andreas Joseph Krogh <andreak(at)officenet(dot)no>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Making NULL entries appear first when ORDER BY <field>
Date: 2005-02-16 02:31:13
Message-ID: 1108521073.28180.5.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, 2005-02-16 at 00:55 +0000, Andreas Joseph Krogh wrote:

> SELECT start_date, start_time, end_time, title
> FROM onp_crm_activity_log
> WHERE start_date IS NOT NULL
> ORDER BY start_date ASC, start_time ASC;
>
> start_date | start_time | end_time | title
> ---------------------+---------------------+---------------------+-----------
> 2005-02-03 00:00:00 | 2005-02-03 08:00:00 | | Something
> 2005-02-03 00:00:00 | 2005-02-03 09:00:00 | 2005-02-03 12:00:00 | Something
> 2005-02-03 00:00:00 | | | Something
>
>
> Now, as you see, touples with NULL in the "start_time"-field appear "after"
> the others. I would like to make all entries where start_time IS NULL apear
> *before* all the others. Any idea how to achieve this?

how about ORDER BY start_date ,
COALESCE(start_time,'0001-01-01 00:00:00'::timestamp)
?

gnari

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Greg Sabino Mullane 2005-02-16 04:08:30 Re: Making NULL entries appear first when ORDER BY <field> ASC
Previous Message Rosser Schwarz 2005-02-16 02:24:50 Re: Making NULL entries appear first when ORDER BY <field> ASC