Orace query data appear garbled problem solving idea _oracle

Source: Internet
Author: User
Tags character set
Problem Description
Often some friends will encounter, I was clearly entered the correct Chinese, why I am in another computer query but appear garbled? In fact, this is the database in the character set conversion when there is a problem,
The following tests are used to describe the specific situation:

1. Environmental
Oracle Database Character Set:
Connected to Oracle Database 11g Enterprise Edition release 11.2.0.1.0
Connected as Scott
Sql> SELECT * from database_properties WHERE property_name = ' nls_characterset ';
Property_name Property_value DESCRIPTION
------------------------------ -------------------------------------------------------------------------------- - -----------------------------------------------------------------------------
Nls_characterset ZHS16GBK Character Set
Customer service end Character set for Oracle database:
In the registry: Nls_lang=simplified Chinese_china. ZHS16GBK the following figure:

Character set of the operating system where Oracle resides:
Microsoft Windows [version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\users\andy>chcp
Activity code page: 936
The expression is: China-Simplified Chinese (GB2312)

2. Test
The character set is as follows:
Oracle database Character Set: ZHS16GBK
Oracle Database Client Character set: ZHS16GBK
Operating system Character set: China-Simplified Chinese (GB2312)
Enter test data:
Sql> INSERT into Tab_indx
2 values (1, ' character input character set test ', ' Chinese input test ', sysdate);
1 row inserted
The character set is not modified to test the data reality:
Sql> SELECT * from tab_indx where tid = 1;
TID tname Tdesc SYSDT
---------- -------------------------------------------------------------------------------- --------------------- ----------------------------------------------------------- -----------
1 Chinese character input character set test chinese input test 2012/12/30
Display Normal,
Now I'm going to change the character set at the customer service end to: UTF8
and the registration form: nls_lang=simplified Chinese_china. UTF8
Now the character set is as follows:
Oracle database Character Set: ZHS16GBK
Oracle Database Client Character set: UTF8
Operating system Character set: China-Simplified Chinese (GB2312)
Now query the data you just entered:
Sql> select Tname,tdesc from Tab_indx;
Tname Tdesc
-------------------------------------------------------------------------------- ------------------------------- -------------------------------------------------
found that the query data is not normal, because these characters are stored in the ZHS16GBK encoding format, however, you query the Oracle customer service based on the code (UTF8) conversion, and into the UTF8 encoding format, but the operating system is Simplified Chinese (GB2312), So the operating system to the UTF8 encoded data, as Simplified Chinese (GB2312) encoding format data display, the results appear garbled,
Now I'm going to insert another piece of data:
Sql> INSERT into Tab_indx
2 values (1, ' UTF8 characters input character set test ', ' Chinese input test ', sysdate);
1 row inserted
Query again:
Sql> select Tname,tdesc from Tab_indx;
Tname Tdesc
-------------------------------------------------------------------------------- ------------------------------- -------------------------------------------------
found that the new inserted data also appeared garbled, but garbled and just the value is not the same?? Why, then?
Because the input of Chinese characters, is the Simplified Chinese (GB2312) encoding format, when the Oracle database in accordance with the client's encoding format to the database, Oracle database found that the Oracle database client is UTF8 encoding format, and the database encoding format (ZHS16GBK) is not the same , the character set conversion, UTF8-->ZHS16GBK, so the Simplified Chinese (GB2312) encoding data as UTF8, into the ZHS16GBK coding format data, has been wrong, the query out of natural conversion back will not be
(It would have been necessary to test the situation with the character set of the Oracle database, which is temporarily not tested) Here we are modifying the character set of the Oracle customers back;
In the registry: Nls_lang=simplified Chinese_china. Zhs16gbk
now the character set is as follows
Oracle database Character Set: ZHS16GBK
Oracle Database Client Character set: ZHS16GBK
Operating system Character set: China-Simplified Chinese (GB2312)
Query again:
Sql> select Tname,tdesc from Tab_indx;
Tname Tdesc
-------------------------------------------------------------------------------- ------------------------------- -------------------------------------------------
Chinese character input character set test chinese input test
UTF8????????????? Chinese Input Test
found that the initial input of the Chinese characters are normal, but the second input of the Chinese characters, and changed, and the last garbled different???
Although there is no encoding format conversion, but the last time the data is stored in the wrong encoding format, so the display is certainly incorrect
In fact, there are several cases of testing, due to the limitations of local environment, so the test of other circumstances, we can go to try, such as: the character set of the database is UTF8, and then customer service end of the character set changes, the Chinese character input and output have any effect
Based on the above test situation and my own analysis, now summarized as follows:
1. The database query data, is the Oracle database character set, the Oracle client character set, the operating system character set together effect result.
The 2.Oracle data and query data are converted through the character set of the Oracle database and the character set of the Oracle customer service, and the display data is determined according to the character set of the operating system.
3. To avoid garbled characters, you must set the character set of the Oracle client to the same as the operating system.

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.