Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort an alphabetical text field?

Tags:

People also ask

How do I sort a text field in SQL?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

How do you arrange things in alphabetical order?

Rule 1. – Alphabetic Order alphabetize names by comparing the first unit letter by letter. If the first letters are the same, file in terms of the second letter, and so on.


I have a couple of records that need to be sorted but the field is varchar. They use the English Alphabet from A-Z. After Z it goes from AA, AB, AC, etc… for example:

CREATE TABLE #foo(x VARCHAR(30));

INSERT #foo(x) SELECT 'A'
UNION SELECT 'AA'
UNION SELECT 'Z'
UNION SELECT 'B'
UNION SELECT 'AB'
UNION SELECT 'BB';

The ordering I want in the result is:

A
B
Z
AA
AB
BB

I know I SHOULD use a number field and sort by that, but at the moment I don’t have that option. I am using SQL Server and the front end is on a report in Access 2010.