Dev Helpers

SQL / Sorting and paging

Sort numeric text as numbers

Dev Helpers · January 21, 2026

Text sorting places character sequences in lexical order. Cast numeric text deliberately when the ordering represents a quantity.

Worked example

WITH labels(v) AS (VALUES ('2'),('10'),('1')) SELECT v FROM labels ORDER BY cast(v AS INTEGER);

Result

[
  {
    "v": "1"
  },
  {
    "v": "2"
  },
  {
    "v": "10"
  }
]

Implementation notes

Casting malformed numeric text can produce a plausible numeric result. Validate imported fields before treating the sort order as proof of clean data.

Try it yourself

Sort the strings 2, 10 and 02 and decide how to resolve numeric ties.

Open the SQL example →

Editorial date January 21, 2026 · Published December 6, 2026

Related articles

Compare two literal lists

Set operations compare rows under SQL's rules for duplicates. Check whether the result represents membership or the number of occurrences.

Count jobs per active day

Grouping timestamps by day produces counts only for days present in the data. A continuous chart may need a calendar series as well.