Oracle uses orderby to sort Chinese characters by multiple fields

Source: Internet
Author: User
Today, I encountered a strange problem. I executed the same SQL statement in two different databases, but the sorting results of the same data were inconsistent. Run the following SQL statement: selectdecode (brch. LOCAL, Y, domestic, international) aslocal, brch. COUNTRY, brch. PROVINCE, brch. CITY, brch. AREA, brch. VENDOR, brch. SERVI

Today, I encountered a strange problem. I executed the same SQL statement in two different databases, but the sorting results of the same data were inconsistent. Run the following SQL statement: select decode (brch. LOCAL, 'y', 'China', 'internal') as local, brch. COUNTRY, brch. PROVINCE, brch. CITY, brch. AREA, brch. VENDOR, brch. SERVI

Today, I encountered a strange problem. I executed the same SQL statement in two different databases, but the sorting results of the same data were inconsistent.

Run the following SQL statement:

Select decode (brch. LOCAL, 'y', 'internal', 'internal') as local,
Brch. COUNTRY,
Brch. PROVINCE,
Brch. CITY,
Brch. AREA,
Brch. VENDOR,
Brch. SERVICE_PROVIDER,
Brch. SERVICE_SITE_ADDR
From lpmsrepdata. vip_sx_service_site_brch brch
Where brch. type_cd = 'tjjg'
And brch. active_flg = 'y'
AND brch. PROVINCE = trim ('hubei ')
Order by brch. local,
Brch. country,
Brch. province,
Brch. city,
Brch. area,
Brch. service_provider,
Brch. service_site_addr asc

The execution results are;

1.

Local country province city area vendor SERVICE_PROVIDER SERVICE_SITE_ADDR
------ Too many threads have been uploaded --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
China (Hubei) Wuhan Hanyang district, Wuhan (former Bo health check Center)-1-2 floor, Hanjiang Sunshine City, No. 19, erqiao Road, Hanyang district, Wuhan (former Bo health check Center)
China's Hubei Wuhan Jiang 'an district Mei Nian da healthcare-1-3 floor, Huaqiao building, No. 107, huangxiaohe Road, Jiang 'an district, Wuhan, China (former mengtian Lake Hotel)
China's Hubei Wuhan Wuchang District Mei Nian da healthcare-4F guangze center, No. 108, zhongbei Road, Wuchang district, Wuhan, China

2.


Local country province city area vendor SERVICE_PROVIDER SERVICE_SITE_ADDR
------ Too many threads have been uploaded --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
China's Hubei Wuhan Wuchang District Mei Nian da healthcare-4F guangze center, No. 108, zhongbei Road, Wuchang district, Wuhan, China
China (Hubei) Wuhan Hanyang district, Wuhan (former Bo health check Center)-1-2 floor, Hanjiang Sunshine City, No. 19, erqiao Road, Hanyang district, Wuhan (former Bo health check Center)
China's Hubei Wuhan Jiang 'an district Mei Nian da healthcare-1-3 floor, Huaqiao building, No. 107, huangxiaohe Road, Jiang 'an district, Wuhan, China (former mengtian Lake Hotel)

Checked, oracle sorting rules for order

Oracle provides the following types of sorts:
Binary sort
Monolingual linguistic sort
Multilingual linguistic sort
Note: The last two types can be unified into linguistic sort.

In Chinese,

Chinese is how to sort?

Prior to Oracle9i, the Chinese are in accordance with the sort of binary encoding.
Added in oracle9i in accordance with pinyin, radical, stroke order functions. Set NLS_SORT value
SCHINESE_RADICAL_M in accordance with the radical (first order), stroke (second order) Sort
SCHINESE_STROKE_M in accordance with the stroke (first order), radicals (second order) Sort
SCHINESE_PINYIN_M sorted according to Pinyin

Therefore, you must consider sorting;Database Character Set, NLS_SORT

View the character set of the database;

SQL> select * from v $ nls_parameters where PARAMETER like '% NLS_CHARACTERSET % ';

PARAMETER VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_CHARACTERSET AL32UTF8

SQL> select * from v $ nls_parameters where PARAMETER like '% NLS_CHARACTERSET % ';

PARAMETER VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_CHARACTERSET ZHS16GBK

SQL>

If the character set is ZHS16GBK/ZH16GBK, use order by to sort the Chinese characters in the pinyin order by default. If it is other (such as UTF8), the Chinese characters are sorted in BINARY.

How can I sort a database character set by Chinese pinyin when it is not a Chinese character set? The answer is to set NLS_SORT

But the nls_sort is indeed the same.

SQL> select value from nls_database_parameters where parameter = 'nls _ SORT ';

VALUE
--------------------------------------------------------------------------------
BINARY

SQL> select value from nls_database_parameters where parameter = 'nls _ SORT ';

VALUE
--------------------------------------------------------------------------------
BINARY


When the character set is a Chinese Character Set and the NLS_SORT is BINARY, Chinese characters are sorted by Chinese characters and Pinyin;
When the character set is not a Chinese character set (such as UTF8) or NLS_SORT is BINARY, Chinese characters are sorted by BINARY encoding.
Therefore, for non-Chinese Character Set libraries: You can set its NLS_SORT to customize the sorting of Chinese characters; change the NLS_SORT of the current session:

ALTER session SET NLS_SORT = SCHINESE_PINYIN_M;

Let's look at the results again, and they will all be consistent ..

Local country province city area vendor SERVICE_PROVIDER SERVICE_SITE_ADDR
----- Begin certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
China (Hubei) Wuhan Hanyang district, Wuhan (former Bo health check Center)-1-2 floor, Hanjiang Sunshine City, No. 19, erqiao Road, Hanyang district, Wuhan (former Bo health check Center)
China's Hubei Wuhan Jiang 'an district Mei Nian da healthcare-1-3 floor, Huaqiao building, No. 107, huangxiaohe Road, Jiang 'an district, Wuhan, China (former mengtian Lake Hotel)
China's Hubei Wuhan Wuchang District Mei Nian da healthcare-4F guangze center, No. 108, zhongbei Road, Wuchang district, Wuhan, China

Local country province city area vendor SERVICE_PROVIDER SERVICE_SITE_ADDR
----- Begin certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
China (Hubei) Wuhan Hanyang district, Wuhan (former Bo health check Center)-1-2 floor, Hanjiang Sunshine City, No. 19, erqiao Road, Hanyang district, Wuhan (former Bo health check Center)
China's Hubei Wuhan Jiang 'an district Mei Nian da healthcare-1-3 floor, Huaqiao building, No. 107, huangxiaohe Road, Jiang 'an district, Wuhan, China (former mengtian Lake Hotel)
China's Hubei Wuhan Wuchang District Mei Nian da healthcare-4F guangze center, No. 108, zhongbei Road, Wuchang district, Wuhan, China


See the following:

Prior to Oracle9i, the Chinese is based on binary coding to sort. The oracle9i added in accordance with the phonetic, radical, stroke sorting.
1, set parameter values NLS_SORT
SCHINESE_RADICAL_M accordance with the radical (first order), stroke (second order) to sort
SCHINESE_STROKE_M accordance with the stroke (first order), radical (second order) to sort
SCHINESE_PINYIN_M sorted according to Pinyin
2. Session-level settings, modify the default ORACLE field Sort:
According to Pinyin: alter session set nls_sort = SCHINESE_PINYIN_M;
According to stroke: alter session set nls_sort = SCHINESE_STROKE_M;
According to the radical: alter session set nls_sort = NLS_SORT = SCHINESE_RADICAL_M;
3, the statement level is set Sort:
Oracle according to alphabetical order
Select * From [tablename] order By nlssort (colname, 'nls _ SORT = SCHINESE_STROKE_M ');
Oracle accordance with the radical sort
Select * From [tablename] order By nlssort (colname, 'nls _ SORT = SCHINESE_RADICAL_M ');
Oracle sorted according to Pinyin
Select * From [tablename] order By nlssort (colname, 'nls _ SORT = SCHINESE_PINYIN_M ');
4. modify the system parameters (database where the operating system ):
Set NLS_SORT = SCHINESE_RADICAL_M; export NLS_SORT (sh)
Setenv NLS_SORT SCHINESE_RADICAL_M (csh)
HKLC \ SOFTWARE \ ORACLE \ home0 \ NLS_SORT (win registry)

Reference others' experiments:

If the database character set is ZH16GBK, order by is used to sort the Chinese characters in the pinyin order by default. Is there a way to change this default rule?
The answer is yes. Oracle provides three sorting methods for simplified Chinese, mainly centered on "pinyin", "radical", and "pen number.
Through experiments, we will show you the charm of NLSSORT in changing the ordering rules for simplified Chinese characters.

1. Sorting in Oracle official documents is described as follows:
"Linguistic Sorts"
Http://download.oracle.com/docs/cd/B19306_01/server.102/b14225/applocaledata.htm#sthref2000
In the Table "Table A-15 Multilingual LInguistic Sorts", let's take a look at the content about the Chinese sorting (the first three are related to the simplified Chinese sorting, and the last two are related to the traditional Chinese sorting ):
1) SCHINESE_RADICAL_M
Simplified Chinese sort based on radical as primary order and number of strokes order as secondary order
Note: In simplified Chinese, the order is "beginning", and the order is "number of strokes;

2) SCHINESE_STROKE_M
Simplified Chinese sort uses number of strokes as primary order and radical as secondary order
Note: In simplified Chinese, the "number of strokes" in the first order is the "beginning" in the second order;

3) SCHINESE_PINYIN_M
Simplified Chinese PinYin sorting order
Note: Simplified Chinese are sorted by pinyin;

4) TCHINESE_RADICAL_M
Traditional Chinese sort based on radical as primary order and number of strokes order as secondary order
Note: In traditional Chinese, the first order is "beginning", and the second order is "number of strokes;

5) TCHINESE_STROKE_M
Traditional Chinese sort uses number of strokes as primary order and radical as secondary order. It supports supplementary characters.
Note: In traditional Chinese, the "number of strokes" in the first order is the "beginning" in the second order;

2. Create an experiment table T and initialize six records.
Sec @ secooler> create table t (x varchar2 (10 ));
Sec @ secooler> insert into t values ('hou ');
Sec @ secooler> insert into t values ('you ');
Sec @ secooler> insert into t values ('do ');
Sec @ secooler> insert into t values ('late ');
Sec @ secooler> insert into t values ('push ');
Sec @ secooler> insert into t values ('detaching ');
Sec @ secooler> commit;
Sec @ secooler> select * from t;

X
------------------------------
Hou
You
Do
Pull
Push
Split

6 rows selected.

3. Confirm the database version and Character Set
Sec @ secooler> select * from v $ version;

BANNER
----------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0-64bit Production
PL/SQL Release 11.2.0.1.0-Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0-Production
NLSRTL Version 11.2.0.1.0-Production

Sec @ secooler> select userenv ('language') from dual;

USERENV ('language ')
-----------------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK

4. In this environment, view the default Chinese Character sorting rules
Sec @ secooler> select * from t order by x;

X
------------------------------
Split
Hou
Pull
You
Push
Do

6 rows selected.

The default Chinese Character sorting rule is "pinyin ".

5. Use nlssort to force sorting by pinyin
Sec @ secooler> select * from t order by nlssort (x, 'nls _ SORT = SCHINESE_PINYIN_M ');

X
------------------------------
Split
Hou
Pull
You
Push
Do

6 rows selected.

6. Use nlssort to forcibly sort by "beginning" (first order) and "number of strokes" (Second Order)
Sec @ secooler> select * from t order by nlssort (x, 'nls _ SORT = SCHINESE_RADICAL_M ');

X
------------------------------
You
Hou
Do
Split
Pull
Push

6 rows selected.

The Chinese characters next to a single person are placed in front, while the Chinese characters next to a single person are placed in the back; the three Chinese characters next to a single person are further sorted by the number of strokes.

7. Use nlssort to forcibly sort by number of strokes (first order) and "beginning" (Second Order)
Sec @ secooler> select * from t order by nlssort (x, 'nls _ SORT = SCHINESE_STROKE_M ');

X
------------------------------
You
Split
Pull
Hou
Do
Push

6 rows selected.

It can be seen that the number of strokes in the future is more.

8. For details about the NSLSORT function in Oracle official documents, refer
Http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/functions111.htm#SQLRF00678

9. Summary
The NSLSORT function provides a very good sorting solution for international support. It is of great significance in a specific application environment. Make good use of it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.