Desc sort order does not work as expected

I have this query in SQL

SELECT Book FROM Library ORDER BY Book desc 

My desired look

[Expired]Web Publishing Co., Ltd.
[Expired]sBox Co.,Ltd
[Expired]mTertainment
wwww

but the result showed the following:

wwww
[Expired]sBox Co.,Ltd
[Expired]mTertainment
[Expired]Web Publishing Co., Ltd.
+4
source share
2 answers

I see here two questions with your order. Firstly, the bracket character is lexicographically smaller than alphanumeric characters, which is not consistent with what you want. Secondly, you have a case sensitivity problem with your book titles. This makes it sBoxlook more lexicographic than Web, although the old letter appears earlier in the alphabet from a case insensitive point of view.

- . , - , , . , , .

SELECT Book
FROM Library
ORDER BY CASE WHEN REGEXP_LIKE(SUBSTR(Book, 1, 1), '[^A-Za-z0-9]') THEN 0 ELSE 1 END,
         LOWER(Book) DESC

, , , , .. ^[.*]:

ORDER BY CASE WHEN REGEXP_LIKE(Book, '^[[].*[]]') THEN 0 ELSE 1 END,
         LOWER(Book) DESC
+7

"[]"? , , :

SELECT REPLACE('[Expired]sBox Co.,Ltd', '[Expired]', '') as book 
FROM dual UNION
SELECT REPLACE('[Expired]mTertainmentas', '[Expired]', '') as book 
FROM dual UNION
SELECT 'www' as book FROM dual
ORDER BY book DESC

@Tim , REGEX:

SELECT REGEXP_REPLACE('[xxxa]sBox Co.,Ltd', '^[[].*[]]', '') as book 
FROM dual UNION 
SELECT REGEXP_REPLACE('[xxxb]mTertainment', '^[[].*[]]', '') as book 
FROM dual UNION 
SELECT 'www' as book FROM dual 
ORDER BY book DESC
+1

Source: https://habr.com/ru/post/1663731/


All Articles