Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql sort on first character of column

I have this query that sorts on a 2-character column. I would like to sort only on the first character of it. Is this possible using order by____.

like image 631
Kheva Mann Avatar asked Dec 15 '22 11:12

Kheva Mann


1 Answers

The SUBSTR function can return the first character of a string.

SELECT * FROM YourTable ORDER BY SUBSTR( SomeField, 1, 1 )

SUBSTR takes three parameters, the field to sort, the 1-based start position, and the number of characters to return.

Per my comment above, I maintain that there is no meaningful difference in the output of this and

SELECT * FROM YourTable ORDER BY SomeField
like image 162
Bob Kaufman Avatar answered Dec 17 '22 02:12

Bob Kaufman