Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT .... WHERE something equals with Chinese characters

In a C# window application, when I do

select * from myTable where category = '牛奶'

or

select * from myTable where category = 'baby牛奶'

The result does not return any rows.

But

select * from myTable where category = 'baby'

This result returns some rows. Could anyone tell me why please?

Note: in myTable, some category of column has some values with 牛奶 or baby牛奶, and I have no problems displaying Chinese characters on the window application.

牛奶 are Chinese characters.

like image 915
Kam2012 Avatar asked Dec 22 '12 21:12

Kam2012


People also ask

What does select * from do?

An asterisk (" * ") can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause, which indicates the table(s) to retrieve data from.

What are Chinese characters called?

Chinese characters, also known as Hanzi (漢字) are one of the earliest forms of written language in the world, dating back approximately five thousand years. Nearly one-fourth of the world's population still use Chinese characters today. As an art form, Chinese calligraphy remains an integral aspect of Chinese culture.

Is Chinese a Unicode?

The Unicode Standard contains a set of unified Han ideographic characters used in the written Chinese, Japanese, and Korean languages. The term Han, derived from the Chi- nese Han Dynasty, refers generally to Chinese traditional culture.

What does n mean in SQL?

The "N" prefix stands for National Language in the SQL-92 standard, and is used for representing Unicode characters. In the current standard, it must be an upper case , which is what you will typically find implemented in mainstream products.


1 Answers

This is not a C# issue, but a SQL one.

Make sure that the passed in SQL string is interpreted as a Unicode string in SQL by prepending it with N (SQL Server, MySQL):

select * from myTable where category = N'牛奶'

See Constants (Transact-SQL) on MSDN.

Unicode strings

Unicode strings have a format similar to character strings but are preceded by an N identifier (N stands for National Language in the SQL-92 standard). The N prefix must be uppercase. For example, 'Michél' is a character constant while N'Michél' is a Unicode constant. Unicode constants are interpreted as Unicode data, and are not evaluated by using a code page. Unicode constants do have a collation. This collation primarily controls comparisons and case sensitivity. Unicode constants are assigned the default collation of the current database, unless the COLLATE clause is used to specify a collation. Unicode data is stored by using 2 bytes per character instead of 1 byte per character for character data.

like image 131
Oded Avatar answered Oct 11 '22 17:10

Oded