- Home
- Uncategorized
- PostgreSQL: Date Sorting NULLS FIRST
PostgreSQL: Date Sorting NULLS FIRST
Sometimes we will have a date field that is nullable. For instance, sometimes we way way a date field to be null if a certain event never occured for that record. Sometimes we will want to sort the data so that the nulls are the first results.
Let’s say we have a table like this:
CREATE TABLE IF NOT EXISTS "RegularThingsToLookup" ( "thingName" text primary key, "lastLookedUp" timestamp without time zone, );
SELECT thingName FROM "RegularThingsToLookup" order by "lastLookedUp" NULLS FIRST LIMIT 1;