Oracle Length 和 Lengthb 函数说明

一.官网的说明

http://download.oracle.com/docs/cd/E11882_01/server.112/e26088/functions088.htm#SQLRF00658

Purpose

TheLENGTHfunctionsreturn the length ofchar.LENGTHcalculates length usingcharacters as defined by the input character set.

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

LENGTHBusesbytes instead of characters.

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

LENGTHCusesUnicode complete characters.

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

LENGTH2usesUCS2 code points.

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

LENGTH4usesUCS4 code points.

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

charcan beany of the data typeschar,varchar2,nchar,nvarchar2,clob,ornclob.

The exceptionsareLENGTHC,LENGTH2, andLENGTH4, which do not allow chartobe aCLOBorNCLOB. The return value is of data typeNUMBER.Ifcharhas data typeCHAR, then the length includes all trailingblanks. Ifcharis null, then this function returns null.

Restriction on LENGTHB(Lengthb函数的限制)

TheLENGTHBfunctionis supported for single-byte LOBs only. It cannot be used withCLOBandNCLOBdatain a multibyte character set.

Examples

The followingexample uses theLENGTHfunction using a single-byte databasecharacter set:

SELECT LENGTH('CANDIDE') "Length incharacters" FROM DUAL;

Length in characters

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

7

The next example assumes a double-bytedatabase character set.

SELECT LENGTHB ('CANDIDE') "Length inbytes" FROM DUAL;

Length in bytes

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

14

二.示例说明

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

SYS@anqing1(rac1)> SELECT USERENV('LANGUAGE') FROM DUAL;

USERENV('LANGUAGE')

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

AMERICAN_AMERICA.ZHS16GBK

SQL>select length('安庆') from dual;

2

SQL>select lengthb('安庆') from dual;

4

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

6

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

6

通过这个示例,我们可以看出来,Length 和 Lengthb 函数的一个重要用处,就是用来判断记录值里是否有中文内容。

如果有中文,那么Length() != Lengthb()

如果没有中文,那么Length() == Lengthb()

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

Blog: http://blog.csdn.net/tianlesoftware

Weibo: http://weibo.com/tianlesoftware

Email: [email protected]

DBA1 群:62697716(满); DBA2 群:62697977(满)DBA3 群:62697850(满)

DBA 超级群:63306533(满); DBA4 群:83829929(满) DBA5群: 142216823(满)

DBA6 群:158654907(满) DBA7 群:69087192(满)DBA8 群:172855474

DBA 超级群2:151508914 DBA9群:102954821 聊天 群:40132017(满)

--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请

你可能感兴趣的:(oracle)