今天作一個需求,要求按照人名排序,最開始沒有怎麼注意,就直接排序了,大體以下。html
select * from test_pinyin order by name
後來發現須要使用拼音排序,oracle
默認的排序是安裝二進制排序的,這一排序方式是按照字符在他所在的編碼表裏面的數值的大小進行排序的,二進制排序是最快的一種排序方式,這一排序方式對英語字母表的排序是合理的,可是對於一些其餘的語言就不那麼合理了。sql
Using Binary Sorts One way to sort character data is based on the numeric values of the characters defined by the character encoding scheme. This is called a binary sort. Binary sorts are the fastest type of sort. They produce reasonable results for the English alphabet because the ASCII and EBCDIC standards define the letters A to Z in ascending numeric value. When characters used in other languages are present, a binary sort usually does not produce reasonable results. For example, an ascending ORDER BY query returns the character strings ABC, ABZ, BCD, ÄBC, when Ä has a higher numeric value than B in the character encoding scheme. A binary sort is not usually linguistically meaningful for Asian languages that use ideographic characters數據庫
對於漢字使用拼音來排序的場景仍是有不少的,所以特將漢字使用拼音的排序方式記錄一下。session
- 方法一:語句級別設置排序方式
select * from test_pinyin order by NLSSORT(name,'NLS_SORT = SCHINESE_PINYIN_M')
- 方法二:設置數據庫的排序參數
-- 修改系統設置 alter system set nls_sort='SCHINESE_PINYIN_M' scope=spfile
- 方法三:修改session
-- 修改session alter SESSION set NLS_SORT = SCHINESE_PINYIN_M
推薦使用第一種方法。oracle
怎麼查看系統的默認排序方式呢?咱們能夠查相應的字典表,萬能的字典。ide
-- 查字典,找到關於NLS(National Language Support)的表 select * from dict where TABLE_NAME like '%NLS%' -- 查session的NLS排序參數 select * from NLS_SESSION_PARAMETERS WHERE PARAMETER = upper('nls_sort')
參考
1.Oracle中針對中文進行排序 2.Oracle® Database Globalization Support Guide 11g Release 2 (11.2)ui