[thelist] SQL Help needed

Joshua Olson joshua at alphashop.net
Thu Jan 31 19:12:00 CST 2002


----- Original Message -----
From: "Kevin Greene" <sinnot at iol.ie>
Subject: [thelist] SQL Help needed


> Hi,
>
>    I have a small problem with a SQL statement.  Basically I have a table
with
> two datetime fields in it.  They are called "paiduntil" and
"lastloggedon".  If
> "paiduntil" is not null, I want these results displayed first and sorted
by
> "lastloggedon", then I want the records where "paiduntil" is null ordered
by
> "lastloggenon"

You need the "case.. when" statement for this:

SELECT
  ordering = CASE
     WHEN paiduntil IS NULL THEN 1
     ELSE 0
   END,
  *
FROM TableA
ORDER BY 1, lastloggedon

Or something like that.  The "order by 1" tells the DB to order by the first
column, which in this case is a select statement that returns 1 if the field
is null, and 0 otherwise.

HTH

-joshua




More information about the thelist mailing list