Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

varchar2(100 char) vs varchar2(100 bytes)

947771Mar 13 2013 — edited Mar 13 2013
hi,

i am using oracle 11g express r2

while making tables i show, one can make columns like varchar2(100 char) or varchar2(100 bytes)

i chose varchar2(100 CHAR) because i wanted to store only 100 characters.

but in stored proc , when i declare any variable to hold value of above column, i declare varchar2(100).

is it good,

please tel me which approach i should follow in above case.

yours sincerely

Edited by: 944768 on Mar 13, 2013 3:23 AM

Edited by: 944768 on Mar 13, 2013 5:10 AM

Comments

Chanchal Wankhade
Hi,


Let us assume the database character set is UTF-8, which I believe is the default in recent version of Oracle. In this case, some characters take more than 1 byte to store in the database.

If you define the field as VARCHAR2(11 BYTE), Oracle will allocate 11 bytes for storage, but you may not actually be able to store 11 characters in the field, because some of them take more than one byte to store, e.g. non-English characters.

By defining the field as VARCHAR2(11 CHAR) you tell Oracle to allocate enough space to store 11 characters, no matter how many bytes it takes to store each one. I believe that in Oracle 10g, 3 bytes per character were used.


Also find good links.

http://stackoverflow.com/questions/81448/difference-between-byte-and-char-in-column-datatypes
2379822
theoa
Declare your variables as table.column%TYPE.
Even if the type (length) of the database column changes, it will still fit in the variable.
BluShadow
Chanchal Wankhade wrote:
Hi,


Let us assume the database character set is UTF-8, which I believe is the default in recent version of Oracle. In this case, some characters take more than 1 byte to store in the database.

If you define the field as VARCHAR2(11 BYTE), Oracle will allocate 11 bytes for storage, but you may not actually be able to store 11 characters in the field, because some of them take more than one byte to store, e.g. non-English characters.

By defining the field as VARCHAR2(11 CHAR) you tell Oracle to allocate enough space to store 11 characters, no matter how many bytes it takes to store each one. I believe that in Oracle 10g, 3 bytes per character were used.
It could be up to 4 bytes depending on the character set and the character being stored.

Also need to consider that if using a multi byte character set, then the limit on varchar2 columns on the database is still 4000 bytes and not 4000 characters, so if multi-byte characters get stored, the number of characters that one can store in that column can be as few as 1000. It can cause confusion when some people think the limit is 4000 characters and then find they have trouble storing that many because of multi-byte characters. (Same principle applies to the 32767 byte limit in PL/SQL varchar2 variables)
Umesh P
All the three options
1) varchar2(100 char)
2) varchar2(100 bytes)
3) varchar2(100)

are equally supported and recommended. Using any of them will result in insertion of 100 characters only.
BluShadow
992981 wrote:
All the three options
1) varchar2(100 char)
2) varchar2(100 bytes)
3) varchar2(100)

are equally supported and recommended. Using any of them will result in insertion of 100 characters only.
No it won't. Clearly you haven't read previous replies.

varchar2(100 bytes) will support at most 100 characters, but could be as few as 25 characters if it is populated with multi byte characters (each could take up to 4 bytes).
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 10 2013
Added on Mar 13 2013
5 comments
47,917 views