Oracle中文字符排序规则详解 NLS_SORT

版本要求 Oracle 9i及以上


一、函数名 NLSSORT

 

二、方法参数 NLS_SORT

参数值:

1、SCHINESE_RADICAL_M  按照部首(第一顺序)、笔划(第二顺序)排序

2、SCHINESE_STROKE_M  按照笔划(第一顺序)、部首(第二顺序)排序

3、SCHINESE_PINYIN_M  按照拼音排序

 

三、基本语法

 -- 拼音
SELECT * FROM 表名 ORDER BY NLSSORT(字段名,'NLS_SORT = SCHINESE_PINYIN_M');

-- 笔划
SELECT * FROM 表名 ORDER BY NLSSORT(字段名,'NLS_SORT = SCHINESE_STROKE_M');

-- 部首
SELECT * FROM 表名 ORDER BY NLSSORT(字段名,'NLS_SORT = SCHINESE_RADICAL_M');

 

四、拼音比较细则

    ① 按位取得要比较的两个字符

    ② 英文与英文比较,按 a-A-z-Z 的顺序排列,与Oracle默认顺序 A-Za-z 不同

    ③ 英文与中文比较,英文正序在前

    ④ 中文与中文比较,先转全拼大写英文,再以A-Z的顺序按位比较

    ⑤ 同声字比较,平声正序在前,仄声正序在后,即 c-Ch-s-Sh-z-Zh

    ⑥ 平仄相同的同声字比较,按音调 “阴阳上去” 的顺序排列,即阴平(ˉ)、阳平(ˊ)、上声(ˇ)、去声(ˋ)

 

1、英文与英文排序

-- 英文排序
SELECT name FROM
(
    (SELECT 'Z' AS name FROM dual)
    UNION
    (SELECT 'a' AS name FROM dual)
    UNION
    (SELECT 'A' AS name FROM dual)
    UNION
    (SELECT 'z' AS name FROM dual)
)
ORDER BY NLSSORT (
    name,
    'NLS_SORT = SCHINESE_PINYIN_M'
);

执行结果:

默认英文字母排序规则是 A-Za-z

SELECT name FROM
(
    (SELECT 'z' AS name FROM dual)
    UNION
    (SELECT 'A' AS name FROM dual)
    UNION
    (SELECT 'Z' AS name FROM dual)
    UNION
    (SELECT 'a' AS name FROM dual)
)
ORDER BY name;

执行结果:

 

2、英文与中文排序

-- 中英排序
SELECT name FROM
(
    (SELECT '额' AS name FROM dual) -- 额    é
    UNION
    (SELECT 'a' AS name FROM dual)
    UNION
    (SELECT 'A' AS name FROM dual)
    UNION
    (SELECT 'E' AS name FROM dual)
    UNION
    (SELECT 'e' AS name FROM dual)
    UNION
    (SELECT 'Z' AS name FROM dual)
    UNION
    (SELECT 'z' AS name FROM dual)
)
ORDER BY NLSSORT (
    name,
    'NLS_SORT = SCHINESE_PINYIN_M'
);

执行结果:

 

3、中文与中文排序

(1)读音

-- 读音
SELECT name FROM
(
    (SELECT '额' AS name FROM dual) -- 额    é
    UNION
    (SELECT '啊' AS name FROM dual) -- 啊    a  ā  á  ǎ  à
)
ORDER BY NLSSORT (
    name,
    'NLS_SORT = SCHINESE_PINYIN_M'
);

执行结果:

 

(2)声调

-- 音调排序
SELECT name FROM
(
    (SELECT '细' AS name FROM dual) -- 细    xì
    UNION
    (SELECT '习' AS name FROM dual) -- 习    xí
    UNION
    (SELECT '玺' AS name FROM dual) -- 玺    xǐ
    UNION
    (SELECT '夕' AS name FROM dual) -- 夕    xī
)
ORDER BY NLSSORT (
    name,
    'NLS_SORT = SCHINESE_PINYIN_M'
);

执行结果:

Oracle中文字符排序规则详解 NLS_SORT_第1张图片

 

(3)平仄声

-- 平仄
SELECT name FROM
(
    (SELECT '吒' AS name FROM dual) -- 吒    zhā  zhà
    UNION
    (SELECT '杂' AS name FROM dual) -- 杂    zá
)
ORDER BY NLSSORT (
    name,
    'NLS_SORT = SCHINESE_PINYIN_M'
);

执行结果:

 

(4)按位比较

-- 按位比较
SELECT name FROM
(
    (SELECT '则' AS name FROM dual) -- 则    zé
    UNION
    (SELECT 'ZF' AS name FROM dual)
)
ORDER BY NLSSORT (
    name,
    'NLS_SORT = SCHINESE_PINYIN_M'
);

执行结果:

说明:"则" 中的 '' 与 "ZF" 中的 'Z' 位序相同,'' 先转大写英文 "ZE" ,再与 "ZF" 中的 'Z' 比较,比较结果有定论,就不再继续比较 "则" 中的 '' 和 "ZF" 中的 'F'

 

(5)多音字

-- 多音字排序
SELECT name FROM
(
    (SELECT '重' AS name FROM dual) -- 重    chóng    zhòng
    UNION
    (SELECT '啊' AS name FROM dual) -- 啊    a    ā    á    ǎ    à
    UNION
    (SELECT '行' AS name FROM dual) -- 行    háng    xíng
)
ORDER BY NLSSORT (
    name,
    'NLS_SORT = SCHINESE_PINYIN_M'
);

执行结果:

 

五、部首比较细则

//TODO

 

 

你可能感兴趣的:(程序/SQL/概念&分析)