Too long field: not all Chinese characters occupy 2 bytes

Source: Internet
Author: User

In Oracle, the field type is often set to varchar2.

In the past, we thought that each Chinese Character occupies 2 bytes, that is, varchar2 (30) can only store 15 Chinese or 30 English characters.

Today, I accidentally discovered that this is actually related to Oracle configuration and can be queried using the following statement:
Select * from V $ nls_parameters t where T. Parameter = 'nls _ characterset ';
You can query the character set of the current database. If value = gb2312, a Chinese character occupies 2 bytes. If value = al32utf8, a Chinese character occupies 3 bytes.

In Java, you can use the following method to obtain the length of a string:

Private int getlength (ncstextfield t ){
Try {
Int ilength = new string (T. gettext (). getbytes ("utf8"), "iso-8859-1"). Length ();
Return ilength;
} Catch (exception e ){
Return T. gettext (). Length ();
}
}

In this way, a Chinese character occupies three bytes,

If you change utf8 to gb2312, one Chinese Character occupies 2 bytes.

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.