The difference between oracle--26substr, substrs, length, lengthb

I remember that I once made such a mistake when developing a form. For a field in the form, corresponding to a field of a table in the database, assuming that this field in the database generally uses the length of 20 Chinese characters, later When I was developing the form, when I set the length of the item type, I set it to 50 bytes, thinking that even if it is 20 Chinese characters, it will take up to 40 bytes at most. However, just because this one took it for granted, an error occurred. Later, it was found that the database character set encoding was utf8, so it should be set to 60. Since then, every time it comes to setting the length of a field, I will pay special attention to what encoding it is.
In oracle, the more common ones may be length and substr. At least most of what I see are these two. If I didn't find lengthb and substrb in the code yesterday, I guess I forgot. length indicates the character length of the string, lengthb indicates the byte length of the string; substr indicates that the substring is obtained according to the character length, and substrb indicates that the string is obtained according to the byte length. Let's look directly at the example to illustrate:
SELECT length('Ye Dehua abc') -- length is counted in characters, Chinese characters, English and numbers are all 1 character, so 6
  FROM dual is returned here;
SELECT lengthb('Ye Dehua abc') -- Length is measured in bytes, I am UTF -8 encoding here, 3 bytes for Chinese characters, and 1 byte for English, so 12 is returned here
  .
, return: Ye Dehua a
              1,
              4)
  FROM dual;
SELECT substrb(' Ye Dehua abc',
               1,
               2) -- substrb is intercepted by bytes, 2 is less than one Chinese character length, return: two spaces
  FROM dual;
SELECT substrb('Ye Dehua abc',
               1,
               3) -- substrb is intercepted by bytes, 3 is exactly one Chinese character length, return: Ye
  FROM dual;
SELECT substrb('Ye Dehua abc',
               1,
               4) -- substrb is intercepted by bytes, if 4 is more than one Chinese character and less than two Chinese characters, return: Ye add a space
  FROM dual;
in oracle There should be other similar methods in it, so I won't summarize it here, but that's probably what it means. To add, it is said that the default length when defining a character type in oracle is byte. For example, varchar2(20) represents a length of 20 bytes. If it is to be defined as a character, then varchar2(20 char), but generally we also Both are defined in bytes.

The details are as follows:
LENGTH(string1) returns the length in characters.
LENGTHB(string1) returns the length in bytes.
LENGTHC(string1) returns the length in Unicode complete characters.
LENGTH2(string1) returns the length in UCS2 The length in code points.
LENGTH4(string1) Returns the length in UCS4 code points.

In different databases, the value obtained by LENGTHB may be different.
For example, LENGTHB('ha') may get a value of 2 or 3 in different databases.
Because of different character sets, the encoding of Chinese characters is different.
For example, ZHS16GBK is two bits, and two bytes are used to define a Chinese character.
In UTF8, 3 bytes are used. (There is also the possibility of UTF8 with 2 Chinese characters)
In short, the value of lengthb is related to the character set of your current database.


The biggest difference between length() and lengthb() in oracle is:
SQL> select length('Happy Chinese New Year') from dual;

LENGTH('Happy Chinese New Year')
---------------- --
4

SQL> select lengthb('Happy Chinese New Year') from dual;

LENGTHB('Happy Chinese New Year')
-------------------
8

Difference: length is a character Length,
lengthb obtains the length in bytes.

length returns the number of characters
lengthb returns the number of bytes
Chinese character "month" in length, returns 1
and lengthb returns 2
 

 length refers to the number of characters, and lengthb refers to the number of bytes. The number of characters has a lot to do with the database character set. The parameters of length and lengthb are both varchar2 type, so length(sysdate) has an implicit type conversion, which is actually equivalent to length(to_char(sysdate)). After ORACLE is installed, the default NLS_DATE_FORMAT parameter value is DD-MON-RR , the result is equivalent to length('28-September-05') and
lengthb('28-September-05'), the result is 9 and 10




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803936&siteId=291194637