Sorting Using Substrings of Column Values

6.12.1 Problem

You want to sort a set of values using one or more substrings of each value.

6.12.2 Solution

Extract the hunks you want and sort them separately.

6.12.3 Discussion

This is an application of sorting by expression value (Recipe 6.4). If you want to sort records using just a particular portion of a column's values, extract the substring you need and use it in the ORDER BY clause. This is easiest if the substrings are at a fixed position and length within the column. For substrings of variable position or length, you may still be able to use them for sorting if there is some reliable way to identify them. The next several recipes show how to use substring-extraction to produce specialized sort orders.

Категории