浅析NLS_SORT、NLS_COMP( 二)

在继续NLS _SORT 、NLS_COMP之前,我们首先来学习一个函数nlssort.

nlssort函数是ORACLE为我们提供的可以改变默认排序和比较规则的一个函数。其语法格式如下:

char参数代表将要进行比较排序的字符串,而nlsparam参数则控制着比较排序的规则。如果没有制定nlsparam参数,则默认采用当前session下的nls_comp和nls_sort的取值。nlsparam的参数格式为‘NLS_SORT=sort’。NLSSORT函数的返回值是RAW类型,因为raw类型具有2000字节的限制,因此如果char参数的长度超个2000字节,则截取最考前的2000字节进行排序操作,这时的结果可能并不是我们所预期的。

nlsparam的取值可以通过查询v$nls_valid_values获得:

SQL> select parameter,value from v$nls_valid_values where parameter='SORT' order by value;

PARAMETER		       VALUE
------------------------------ ------------------------------
SORT			       ARABIC
SORT			       ARABIC_ABJ_MATCH
SORT			       ARABIC_ABJ_SORT
SORT			       ARABIC_MATCH
SORT			       ASCII7
SORT			       AZERBAIJANI
SORT			       BENGALI
SORT			       BIG5
SORT			       BINARY
SORT			       BULGARIAN
SORT			       CANADIAN FRENCH

PARAMETER		       VALUE
------------------------------ ------------------------------
SORT			       CANADIAN_M
SORT			       CATALAN
SORT			       CROATIAN
SORT			       CZECH
SORT			       CZECH_PUNCTUATION
SORT			       DANISH
SORT			       DANISH_M
SORT			       DUTCH
SORT			       EBCDIC
SORT			       EEC_EURO
SORT			       EEC_EUROPA3

PARAMETER		       VALUE
------------------------------ ------------------------------
SORT			       ESTONIAN
SORT			       FINNISH
SORT			       FRENCH
SORT			       FRENCH_M
SORT			       GBK
SORT			       GENERIC_BASELETTER
SORT			       GENERIC_M
SORT			       GERMAN
SORT			       GERMAN_DIN
SORT			       GREEK
SORT			       HEBREW

PARAMETER		       VALUE
------------------------------ ------------------------------
SORT			       HKSCS
SORT			       HUNGARIAN
SORT			       ICELANDIC
SORT			       INDONESIAN
SORT			       ITALIAN
SORT			       JAPANESE
SORT			       JAPANESE_M
SORT			       KOREAN_M
SORT			       LATIN
SORT			       LATVIAN
SORT			       LITHUANIAN

PARAMETER		       VALUE
------------------------------ ------------------------------
SORT			       MALAY
SORT			       NORWEGIAN
SORT			       POLISH
SORT			       PUNCTUATION
SORT			       ROMANIAN
SORT			       RUSSIAN
SORT			       SCHINESE_PINYIN_M
SORT			       SCHINESE_RADICAL_M
SORT			       SCHINESE_STROKE_M
SORT			       SLOVAK
SORT			       SLOVENIAN

PARAMETER		       VALUE
------------------------------ ------------------------------
SORT			       SPANISH
SORT			       SPANISH_M
SORT			       SWEDISH
SORT			       SWISS
SORT			       TCHINESE_RADICAL_M
SORT			       TCHINESE_STROKE_M
SORT			       THAI_DICTIONARY
SORT			       THAI_M
SORT			       THAI_TELEPHONE
SORT			       TURKISH
SORT			       UKRAINIAN

PARAMETER		       VALUE
------------------------------ ------------------------------
SORT			       UNICODE_BINARY
SORT			       VIETNAMESE
SORT			       WEST_EUROPEAN
SORT			       XAZERBAIJANI
SORT			       XCATALAN
SORT			       XCROATIAN
SORT			       XCZECH
SORT			       XCZECH_PUNCTUATION
SORT			       XDANISH
SORT			       XDUTCH
SORT			       XFRENCH

PARAMETER		       VALUE
------------------------------ ------------------------------
SORT			       XGERMAN
SORT			       XGERMAN_DIN
SORT			       XHUNGARIAN
SORT			       XPUNCTUATION
SORT			       XSLOVAK
SORT			       XSLOVENIAN
SORT			       XSPANISH
SORT			       XSWISS
SORT			       XTURKISH
SORT			       XWEST_EUROPEAN

87 rows selected.



通过以上查询获得的SORT的有效取值,均可以为其添加_CI或者_AI后缀,前者表示不区分大小写,后者表示不区分读音。下面是示例:

SQL> select * from tab1;

	ID NAME
---------- --------------------
	 1 a
	 2 A
	 3 B
	 4 b

SQL> select id,name,nlssort(name,'NLS_SORT=BINARY') c1,nlssort(name,'NLS_SORT=BINARY_CI') c2 FROM TAB1 ORDER BY nlssort(name,'NLS_SORT=BINARY');

	ID NAME 		C1		     C2
---------- -------------------- -------------------- --------------------
	 2 A			4100		     6100
	 3 B			4200		     6200
	 1 a			6100		     6100
	 4 b			6200		     6200

SQL> select id,name,nlssort(name,'NLS_SORT=BINARY') c1,nlssort(name,'NLS_SORT=BINARY_CI') c2 FROM TAB1 ORDER BY nlssort(name,'NLS_SORT=BINARY_CI');

	ID NAME 		C1		     C2
---------- -------------------- -------------------- --------------------
	 1 a			6100		     6100
	 2 A			4100		     6100
	 3 B			4200		     6200
	 4 b			6200		     6200

通过这里我看也可以看出,order by 排序是按照NLSSORT的返回值的大写来进系的。


你可能感兴趣的:(浅析NLS_SORT、NLS_COMP( 二))