Oracle中文數字排序


Posted by Vanchy on 2022-04-14

*問題:

User輸入Excel資料的時候,會輸入中文的「一、二、三、四、五、六、七、八、九」
Parsing到資料庫時,為了確保資料跟User輸入的一樣,也是以中文儲存
但在下載時,就會遇到排序有問題的情況
例如:三會排在二前面

ORDER

*解決方式

使用Oracle Function:translate

WITH temp AS
(
    SELECT '一' AS ch
    FROM dual
    UNION ALL
    SELECT '二' AS ch
    FROM dual
    UNION ALL
    SELECT '三' AS ch
    FROM dual
    UNION ALL
    SELECT '四' AS ch
    FROM dual
    UNION ALL
    SELECT '五' AS ch
    FROM dual
    UNION ALL
    SELECT '六' AS ch
    FROM dual
    UNION ALL
    SELECT '七' AS ch
    FROM dual
    UNION ALL
    SELECT '八' AS ch
    FROM dual
    UNION ALL
    SELECT '九' AS ch
    FROM dual
)
SELECT *
FROM temp
ORDER BY translate(combination_seq, '一二三四五六七八九', '123456789');

*缺點

只適用1~9以內的中文字,若為十以上,則要另外處理,很麻煩

*參考資料

  1. Oracle translate用法
  2. 網友解法

#oracle #CHINESE ORDER







Related Posts

CSS保健室|font

CSS保健室|font

分辨 Slice()、Split()、Splice()

分辨 Slice()、Split()、Splice()

00. 前言

00. 前言


Comments