Oracle中 length 和 lengthb 区别

length函数 <wbr>,length <wbr>和 <wbr>lengthb <wbr>区别

Purpose

The LENGTH functionsreturn the length of char. LENGTH calculates length usingcharacters as defined by the input character set.

     --返回以字符为单位的长度.

LENGTHB usesbytes instead of characters.

     --返回以字节为单位的长度.

LENGTHC usesUnicode complete characters.

     --返回以Unicode完全字符为单位的长度.

LENGTH2 usesUCS2 code points.

     --返回以UCS2代码点为单位的长度.

扫描二维码关注公众号,回复: 1395906 查看本文章

LENGTH4 usesUCS4 code points.

    --返回以UCS4代码点为单位的长度.

 

char can be any of the data types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The exceptions are LENGTHC, LENGTH2, and LENGTH4, which do not allow char to be a CLOB or NCLOB. The return value is of data type NUMBER. If char has data type CHAR, then the length includes all trailing blanks. If char is null, then this function returns null.

Restriction on LENGTHB The LENGTHB function is supported for single-byte LOBs only. It cannot be used with CLOB and NCLOB data in a multibyte character set.

Examples

The following example uses the LENGTH function using a single-byte database character set:

SELECT LENGTH('CANDIDE') "Length in characters"

  FROM DUAL;

Length in characters

--------------------

                   7

The next example assumes a double-byte database character set.

SELECT LENGTHB ('CANDIDE') "Length in bytes"

  FROM DUAL;

 

Length in bytes

---------------

             14

 

SQL> select length('北京') from dual;

LENGTH('北京')

--------------

             2

SQL> select lengthb('北京') from dual;

LENGTHB('北京')

---------------

              6

SQL> select lengthb('BeiJing') from dual;

LENGTHB('BEIJING')

------------------

                 7

SQL> select length('BeiJing') from dual;

LENGTH('BEIJING')

-----------------

                7

 

值得一提的是:

在不同的数据库,因为字符集的不同,LENGTHB得到的值可能会不一样。如ZHS16GBK采用两个byte位来定义一个汉字。而在UTF8,采用3byte

--查看字符集语句

SQL> select * from nls_database_parameters where parameter='NLS_CHARACTERSET';

PARAMETER                      VALUE

猜你喜欢

转载自wang286480403.iteye.com/blog/1488696