oracle拼音排序
今天做一個需求,要求按照人名排序,最開始沒有怎麽註意,就直接排序了,大致如下。
select * from test_pinyin order by name
後來發現需要使用拼音排序,oracle
默認的排序是安裝二進制排序的,這一排序方式是按照字符在他所在的編碼表裏面的數值的大小進行排序的,二進制排序是最快的一種排序方式,這一排序方式對英語字母表的排序是合理的,但是對於一些其他的語言就不那麽合理了。
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
對於漢字使用拼音來排序的場景還是有很多的,因此特將漢字使用拼音的排序方式記錄一下。
方法一:語句級別設置排序方式
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
推薦使用第一種方法。
怎麽查看系統的默認排序方式呢?我們可以查相應的字典表,萬能的字典。
-- 查字典,找到關於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)
oracle拼音排序