Sort by name but ignore quotes?
5 answers
A trick that may be acceptable, and faster than a TRIM () or REPLACE () based solution, is to focus only on the first character that is a quote (or by extension, a non-alphanumeric character).
ORDER BY CASE LEFT(myCol, 1)
WHEN '"' THEN REPLACE(myCol, '"', '')
ELSE myCol
END CASE
In general, for a larger dataset, this kind of processing should be done at load time, for example adding columns for queries rather than columns for display purposes.
+3
source to share
SELECT *
FROM table
ORDER BY CASE WHEN len(col) >= 2
THEN REPLACE(LEFT(COL, 1), '"', '')
+ RIGHT(LEFT(col, LEN(col)-1), LEN(LEFT(col, LEN(col)-1))-1)
+ REPLACE(RIGHT(col, 1), '"', '')
ELSE col
END
in ORDER BY
, I exclude the double quotes by replacing it 'inside' the 1st and last characters with '. Of course, it is only valid when col length> = 2.
+1
source to share