Is there a way to combine ORDER BY
and IS NULL
in sql so that I can order by a column if the column isn't null, but if it is null, order by another column?
Something like:
ORDER BY CASE WHEN Column1 IS NOT NULL THEN Column1 ELSE Column2 END
Same as writing:
ORDER BY COALESCE(Column1, Column2)
Both should work in any sane RDBMS.
Try this
ORDER BY COALESCE(fieldA, fieldB);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With