For last couple of days I've been trying to sort a list of alphanumeric text in a natural order.I found that using the NLS_SORT option can order the list correctly (see this answer). But when trying out that solution I found that it made no difference. The list was still displayed as with a normal ORDER BY query. Please not that a solution involving regex is not an option for me.
For testing purposes I made a table and filled it with some data. When running SELECT name FROM test ORDER BY name ASC
I get the following result:
As you see the the ordering is unnatural. It should be more like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.
The solutions I tried involved setting the nls_sort option.
ALTER SESSION SET nls_sort='BINARY'; -- or BINARY_AI
SELECT name FROM test ORDER BY NLSSORT(name,'NLS_SORT=BINARY') -- or BINARY_AI
It should order the text in the list based on the decimal code of each character as stated in the ASCII table. So I expected it to turn out the right way (as the order in that table is 'space', 'dot', numbers, letters), but it did not change anything. The order is still the same as in the image.
If it is BINARY then the sort order is based on the numeric value of each character, so it's dependant on the database character set
It might have something to do with the character set I'm using, but I'm not sure what wrong with it. Running SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET';
gives me the value AL32UTF8
. Which seems like a slightly extended version of UTF8 (correct me if I'm wrong). I'm running on Oracle database version 11.2.0.4.0.
So can anyone tell me what I'm doing wrong or what I'm missing?
Thanks in advance.