oracle汉字排序
最近項目過程中碰到對于數據進行排序按照升序排序需求。這需求貌似很簡單,我沒多想直接order by columnName。忽略了漢字排序的問題導致bug的出現。
業務要求,按照excel中排序出來的結果。對于漢字excel默認是按照拼音排序的,那么問題來了,oracle對于漢字究竟是如何排序的?
經查閱資料發現,對于oracle對漢字排序的問題網上說法不一。有的說是根據二進制編碼排序的,有的缺說是根據拼音排序的。
自己試了試,肯定默認不是拼音排序。可能是二進制編碼吧。
說下具體怎么改,也是轉載網上的一些方法。
1、語句級別設置排序方式:
? ? ?//按照筆劃排序?
? ?select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_STROKE_M');?
? ?//按照部首排序?
? ?select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_RADICAL_M');?
? ?//按照拼音排序,此為系統的默認排序方式?
? ?select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_PINYIN_M');?
2、Session級別的設置,修改ORACLE字段的默認排序方式:
??????????????按拼音:alter session set nls_sort = SCHINESE_PINYIN_M;
??????????????按筆畫:alter session set nls_sort = SCHINESE_STROKE_M;
??????????????按偏旁:alter session set nls_sort = NLS_SORT=SCHINESE_RADICAL_M;
3、修改系統參數(數據庫所在操作系統):
???????????????set NLS_SORT=SCHINESE_RADICAL_M ;export NLS_SORT (sh)setenv NLS_SORT SCHINESE_RADICAL_M (csh)??HKLCSOFTWAREORACLEhome0NLS_SORT (win注冊表);
我這里只是用來語句級別的就可以了。
回到系統默認排序的問題就是設置數據庫的NLS_SORT 這個參數
通過查看select * from nls_database_parameters; 數據庫參數
我的默認值為BINARY
Oracle 官方說明:
| Property | Description |
| Parameter type | String |
| Syntax | NLS_SORT = { BINARY |?linguistic_definition?} |
| Default value | Derived from NLS_LANGUAGE |
| Modifiable | ALTER SESSION |
| Range of values | BINARY or any valid linguistic definition name |
?
- If the value is BINARY, then the collating sequence for ORDER BY queries is based on the numeric value of characters (a binary sort that requires less system overhead).
-
If the value is a named linguistic sort, sorting is based on the order of the defined linguistic sort. Most (but not all) languages supported by the NLS_LANGUAGE parameter also support a linguistic sort with the same name.
Note:
Setting NLS_SORT to anything other than BINARY causes a sort to use a full table scan, regardless of the path chosen by the optimizer. BINARY is the exception because indexes are built according to a binary order of keys. Thus the optimizer can use an index to satisfy the ORDER BY clause when NLS_SORT is set to BINARY. If NLS_SORT is set to any linguistic sort, the optimizer must include a full table scan and a full sort in the execution plan.
???????? You must use the NLS_SORT operator with comparison operations if you want the linguistic sort behavior.
英文水平有限,大概BINARY?這個意思是二進制編碼排序的設定。
總結
以上是生活随笔為你收集整理的oracle汉字排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: # 20155327 2016-2017
- 下一篇: Python开发培训哪里好