Oracle TRANSLATE函数不适用于ORDER BY

时间:2019-07-12 19:42:01

标签: sql oracle translate

我想在我的Oracle ORDER BY上使用TRANSLATE将列排序为A,a,B,b ... Z,z而不是A-Z,a-z。

如果我在SELECT中应用TRANSLATE,然后对该列进行ORDER BY,则效果很好。在ORDER BY上编码TRANSLATE似乎没有影响。

SELECT a1,  
       translate(a1,
                 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz',
                 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') AS sort_col
FROM (SELECT * 
        FROM (SELECT 'A' AS a1 FROM dual UNION 
              SELECT 'a' AS a1 FROM dual UNION
              SELECT 'B' AS a1 FROM dual UNION
              SELECT '#' AS a1 FROM dual UNION
              SELECT '0' AS a1 FROM dual ) )
--ORDER BY 2 
ORDER BY TRANSLATE(1,
                  'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz',
                  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')

输出顺序应为#,0,A,a,B

1 个答案:

答案 0 :(得分:3)

您没有按列的顺序给Translate函数指定列。只是总是翻译1

用您要排序的1列替换您的订单a1

ORDER BY TRANSLATE(a1,
                  'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz',
                  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')

请注意,如果仅使用它进行排序,则可以通过以下方式以更易读的方式实现相同的目的:

ORDER BY UPPER(a1), a1