Oracle字符串函数Length和Lengthb LENGTHC 函数说明 .
一.官网的说明
http://download.oracle.com/docs/cd/E11882_01/server.112/e26088/functions088.htm#SQLRF00658
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代码点为单位的长度.
LENGTH4 usesUCS4 code points.
--返回以UCS4代码点为单位的长度.
char can beany of the data types char, varchar2, nchar, nvarchar2, clob,or nclob.
The exceptionsare LENGTHC, LENGTH2, and LENGTH4, which do not allow char tobe a CLOB or NCLOB. The return value is of data type NUMBER.If char has data type
CHAR, then the length includes all trailingblanks. If char is null, then this function returns null.
Restriction on LENGTHB (Lengthb函数的限制)
The LENGTHB functionis supported for single-byte LOBs only. It cannot be used with CLOB and NCLOB datain a multibyte character set.
Examples
The followingexample uses the LENGTH function 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(string)计算string所占的字节长度 :返回字符串的长度,单位是字节
length(string)计算string所占的字符长度 :返回字符串的长度,单位是字符
二.示例说明
在不同的数据库,因为字符集的不同,LENGTHB得到的值可能会不一样。如ZHS16GBK采用两个byte位来定义一个汉字。而在UTF8,采用3个byte。
> SELECT USERENV('LANGUAGE') FROM DUAL;
USERENV('LANGUAGE')
----------------------------------------------------
SIMPLIFIED CHINESE_CHINA.AL32UTF8
14:32:57 SQL> select length('简单街') from dual;
LENGTH('简单街')
----------------
3
14:35:38 SQL> select lengthb('简单街') from dual;
LENGTHB('简单街')
-----------------
9
14:35:43 SQL> select lengthc('简单街') from dual;
LENGTHC('简单街')
-----------------
3
14:35:48 SQL> select length2('简单街') from dual;
LENGTH2('简单街')
-----------------
3
14:35:52 SQL> select length4('简单街') from dual;
LENGTH4('简单街')
-----------------
3
14:35:58 SQL> select length('jiandanjie') from dual;
LENGTH('JIANDANJIE')
--------------------
10
14:36:15 SQL> select lengthb('jiandanjie') from dual;
LENGTHB('JIANDANJIE')
---------------------
10
14:36:18 SQL> select lengthc('jiandanjie') from dual;
LENGTHC('JIANDANJIE')
---------------------
10
14:36:25 SQL> select length2('jiandanjie') from dual;
LENGTH2('JIANDANJIE')
---------------------
10
14:36:29 SQL> select length4('jiandanjie') from dual;
LENGTH4('JIANDANJIE')
---------------------
10
通过这个示例,我们可以看出来,Length 和 Lengthb 函数的一个重要用处,就是用来判断记录值里是否有中文内容。
如果有中文,那么Length() != Lengthb()
如果没有中文,那么Length() == Lengthb()