一.官網的說明
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得到的值可能會不一樣。如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: dvd.dba@gmail.com
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資料表空間和資料檔案的關係,否則拒絕申請