SQL Server database Chinese characters are sorted by letters, strokes, first letter of Pinyin

Source: Internet
Author: User
Tags comparison table
SQL Server sorting rules are usually not used many times and may be unfamiliar to many beginners. However, there is a common error: SQL Server databases, when querying cross-database, multi-Table connections, if the default Character Set of the two databases is different, the system will return the following error: unable to solve the sort rule conflict of the sort to operation. I. Error Analysis:

SQL server sorting rules are usually not used many times and may be unfamiliar to many beginners. However, when you encounter an SQL server database that supports cross-database, multi-table join queries, if the default Character Set of the two databases is different, the system returns the following error: the sorting rule conflict of the equal to operation cannot be solved. I. Error Analysis:

SQL server sorting rules are usually not used many times, and many beginners may be unfamiliar, but some
A common error occurs when you query a database that is connected to multiple tables in different databases.
If the default Character Set of the library is different, the system will return the following error:

"The sorting rule conflict for equal to operations cannot be resolved ."

I. Error Analysis:
This error is caused by inconsistent sorting rules. Let's perform a test, for example:
Create table # t1 (name varchar (20) collate albanian_ci_ai_ws, value int)

Create table # t2 (name varchar (20) collate chinese_prc_ci_ai_ws, value int)

After the table is created, execute the connection query:

Select * from # t1 a inner join # t2 B on a. name = B. name

In this way, the error occurs:

Server: Message 446, level 16, status 9, Row 1
The sorting rule conflict of the equal to operation cannot be solved.
To eliminate this error, the simplest way is to specify its sorting rules when the table is connected, so that the error will no longer appear. The statement is written as follows:

Select * from # t1 a inner join # t2 B on a. name = B. name collate chinese_prc_ci_ai_ws

Ii. Sorting rule introduction:

What is a sorting rule? "In microsoft SQL server 2000,
The physical storage of strings is controlled by sorting rules. Sorting rules specify the bit mode and storage of each character
Rules Used for saving and comparing characters. "
Run the following statement in the query analyzer to obtain all the sorting rules supported by SQL server.

Select * from: fn_helpcollations ()

The name of a sorting rule consists of two parts. The first half is the character set supported by this sorting rule.
For example:
Chinese_prc_cs_ai_ws
First half: the unicode character set. The chinese_prc _ pointer sorts unicode in simplified Chinese characters.
The second half of the sorting rule is the suffix meaning:
_ Bin binary sorting
_ Ci (cs) is case sensitive, ci is case insensitive, and cs is case sensitive
_ Whether ai (as) distinguishes stress, ai does not distinguish,
_ Ki (ks) indicates whether Kana is distinguished. ki is not distinguished, and ks is distinguished.
_ Whether wi (ws) is differentiated by width wi and ws

Case Sensitive: select this option if you want to make the comparison between uppercase and lowercase letters different.
Accent differentiation: select this option if you want to treat the comparison as different from the accent and non-accent letters. If this option is selected,
Comparison also treats letters with different accents as unequal.
Kana differentiation: select this option if you want to treat Katakana and katakana as different Japanese syllables.
Width differentiation: select this option if you want to make the comparison between halfwidth and fullwidth characters.


Iii. Application of sorting rules:
SQL server provides a large number of sorting rules for windows and SQL server, but its applications often
Ignored by developers. In fact, it is of great use in practice.

Example 1: sort the content of the table name column in pinyin order:

Create table # t (id int, name varchar (20 ))
Insert # t select 1, medium
Union all select 2, Country
Union all select 3, persons
Union all select 4,

Select * from # t order by name collate chinese_prc_cs_as_ks_ws
Drop table # t
/* Result:
Id name
-------------------------------
4
2 countries
3 persons
Medium
*/

Example 2: sort the content of the table name column by the last name strokes:

Create table # t (id int, name varchar (20 ))

Insert # t select 1, 3
Union all select 2, B
Union all select 3, two
Union all select 4, one
Union all select 5, ten
Select * from # t order by name collate chinese_prc_stroke_cs_as_ks_ws
Drop table # t
/* Result:
Id name
-------------------------------
4yi
2 B
3 II
5th
1 3
*/

4. Application extension of sorting rules in practice
SQL server Chinese Character sorting rules can be sorted by pinyin, strokes, etc. How can we use this function?
To solve Chinese Character problems? Here is an example:

Calculate Chinese Character strokes based on the characteristics of sorting rules

To calculate Chinese Character strokes, we must first prepare for the computation. We know that windows has many Chinese characters and unicode currently
A total of 20902 Chinese characters are included. The unicode value of the simplified gbk code starts from 19968.
First, we first use SQL Server to obtain all Chinese characters without dictionary. We simply use SQL statements
You can get:

Select top 20902 code = identity (int, 19968,1) into # t from syscolumns a, syscolumns B

Use the following statement to obtain all Chinese characters sorted by unicode values:

Select code, nchar (code) as cnword from # t

Then, we use the select statement to sort it by strokes.

Select code, nchar (code) as cnword
From # t
Order by nchar (code) collate chinese_prc_stroke_cs_as_ks_ws, code

Result:
Code cnword
-----------------
19968 I
20008 bytes
20022
20031 bytes
20032 bytes
20033 bytes
20057 B
20058 bytes
20059 bytes
20101 bytes
19969 ding
..........

From the above results, we can clearly see that the code for a Chinese character ranges from 19968 to 20101, from small to large,
The first word of the two Chinese characters is "ding", and the code is 19969, so it will not start again in order. With this result, we can easily
Use SQL statements to obtain the first or last Chinese character of each stroke.
The following statement is used to obtain the last Chinese character:

Create table # t1 (id int identity, code int, cnword nvarchar (2 ))

Insert # t1 (code, cnword)
Select code, nchar (code) as cnword from # t
Order by nchar (code) collate chinese_prc_stroke_cs_as_ks_ws, code


Select a. cnword
From # t1
Left join # t1 B on a. id = B. id-1 and a. code Where B. code is null
Order by a. id

Obtain 36 Chinese characters. Each Chinese character is sorted by chinese_prc_stroke_cs_as_ks_ws sorting rule.
Last Chinese character:

Ma fenglongqi

As shown above, "marker" is the last word after sorting all Chinese characters. "marker" is the last word after sorting all the two Chinese characters.
A word... and so on.
However, it was also found that the strokes behind the 33rd Chinese character "33 strokes" were messy and incorrect. But it doesn't matter. It's better than "success" strokes.
There are only four Chinese characters. We manually add: 35, 36, 39, and 64.

Create a Chinese character stroke table (tab_hzbh ):
Create table tab_hzbh (id int identity, cnword nchar (1 ))
-- Insert the first 33 Chinese Characters
Insert tab_hzbh
Select top 33 a. cnword
From # t1
Left join # t1 B on a. id = B. id-1 and a. code Where B. code is null
Order by a. id
-- Add the last four Chinese Characters
Set identity_insert tab_hzbh on
Go
Insert tab_hzbh (id, cnword)
Select 35, n accept
Union all select 36, n distinct
Union all select 39, n distinct
Union all select 64, n distinct
Go
Set identity_insert tab_hzbh off
Go

So far, we can get the result. For example, we want to get the Chinese character "country" strokes:

Declare @ a nchar (1)
Set @ a = Country
Select top 1 id
From tab_hzbh
Where cnword >=@ a collate chinese_prc_stroke_cs_as_ks_ws
Order by id

Id
-----------
8
(Result: the number of strokes in the Chinese "country" is 8)

All the above preparation processes are only used to write the following function. This function disconnects all the temporary tables and
For general purpose and code transfer convenience, the tab_hzbh table content is written in the statement, and a string of user input is calculated.
Total strokes of Chinese characters:

Create function fun_getbh (@ str nvarchar (4000 ))
Returns int
As
Begin
Declare @ word nchar (1), @ n int
Set @ n = 0
While len (@ str)> 0
Begin
Set @ word = left (@ str, 1)
-- If it is not a Chinese character, the stroke count is 0.
Set @ n = @ n + (case when unicode (@ word) between 19968 and 19968 + 20901
Then (select top 1 id from (
Select 1 as id, n encoded as word
Union all select 2, n distinct
Union all select 3, n horse
Union all select 4, n wind
Union all select 5, n-Dragon
Union all select 6, n Qi
Union all select 7, n turtles
Union all select 8, n teeth
Union all select 9, n distinct
Union all select 10, n distinct
Union all select 11, n
Union all select 12, n distinct
Union all select 13, n distinct
Union all select 14, n
Union all select 15, n distinct
Union all select 16, n distinct
Union all select 17, n distinct
Union all select 18, n distinct
Union all select 19, n distinct
Union all select 20, n distinct
Union all select 21, n distinct
Union all select 22, n distinct
Union all select 23, n distinct
Union all select 24, n distinct
Union all select 25, n distinct
Union all select 26, n distinct
Union all select 27, n distinct
Union all select 28, n distinct
Union all select 29, n distinct
Union all select 30, n distinct
Union all select 31, n distinct
Union all select 32, n distinct
Union all select 33, n distinct
Union all select 35, n distinct
Union all select 36, n distinct
Union all select 39, n distinct
Union all select 64, n distinct
) T
Where word >=@ word collate chinese_prc_stroke_cs_as_ks_ws
Order by id asc) else 0 end)
Set @ str = right (@ str, len (@ str)-1)
End
Return @ n
End

-- Function call instance:
Select dbo. fun_getbh (People's Republic of China), dbo. fun_getbh (People's Republic of China)
 
Execution result: the total number of strokes is 39 and 46, both in simplified and Traditional Chinese.

Of course, you can also change the Chinese characters and strokes in the above "union all" to a fixed table.
Create clustered index for the column, and set the column sorting rule:
Chinese_prc_stroke_cs_as_ks_ws
This is faster. If you are using a big5 code operating system, you have to generate additional Chinese characters in the same way.
However, remember that these Chinese characters are selected using SQL statements rather than manually entered.
It is obtained by searching the dictionary, because the Xinhua Dictionary is different from the unicode Character Set after all, and the result of searching the dictionary will be incorrect.
Yes.

  
Obtain the first letter of the Chinese pinyin alphabet using the sorting rule feature.

Using the same total number of strokes, we can also write a function to calculate the first letter of Chinese pinyin. As follows:

Create function fun_getpy (@ str nvarchar (4000 ))
Returns nvarchar (4000)
As
Begin
Declare @ word nchar (1), @ py nvarchar (4000)
Set @ py =
While len (@ str)> 0
Begin
Set @ word = left (@ str, 1)
-- If it is not a Chinese character, the original character is returned.
Set @ py = @ py + (case when unicode (@ word) between 19968 and 19968 + 20901
Then (select top 1 py from (
Select a as py, n encoded as word
Union all select B, n book
Union all select c, n distinct
Union all select d, n distinct
Union all select e, n distinct
Union all select f, n distinct
Union all select g, n distinct
Union all select h, n distinct
Union all select j, n distinct
Union all select k, n distinct
Union all select l, n distinct
Union all select m, n distinct
Union all select n, n distinct
Union all select o, n distinct
Union all select p, n exposure
Union all select q, n distinct
Union all select r, n distinct
Union all select s, n distinct
Union all select t, n distinct
Union all select w, n rows
Union all select x, n distinct
Union all select y, n distinct
Union all select z, n distinct
) T
Where word >=@ word collate chinese_prc_cs_as_ks_ws
Order by py asc) else @ word end)
Set @ str = right (@ str, len (@ str)-1)
End
Return @ py
End

-- Function call instance:
Select dbo. fun_getpy (People's Republic of China), dbo. fun_getpy (People's Republic of China)
All results are: zhrmghg

If you are interested, you can use the same method to extend it to a function for getting all Chinese characters, and you can even get a full read.
Tone, but most of the categories are full. It is best to use a comparison table to get a full search. The search speed for more than 20 thousand Chinese characters is very fast.
The table can also make full use of the table index.

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.