Oracle的函數vsize和length的區別討論
2024-08-29 13:34:04
供稿:網友
The "length" functions return the length of char. LENGTH calculates length using characters as defined by the input character set.
LENGTHB uses bytes instead of characters. LENGTHC uses Unicode complete characters. LENGTH2 uses UCS2 codepoints. LENGTH4 uses UCS4 codepoints
length函數返回字符的長度,它使用定義好的輸入的字符集計算長度.
lengthb使用bytes代替字符
VSIZE returns the number of bytes in the internal rePResentation of eXPr.
vsize 返回內部表示的字節的數目。internal representation of expr誰能解釋一下。
看sql示例:
select length('adfad合理') "bytesLengthIs" from dual --7
select lengthb('adfad') "bytesLengthIs" from dual --5
select lengthb('adfad合理') "bytesLengthIs" from dual --11
select vsize('adfad合理') "bytesLengthIs" from dual --11
select lengthc('adfad合理')"bytesLengthIs" from dual --7
結論:在utf-8的字符集下
lengthb=vsize
lengthc=length
疑問:中文字符怎么會占用了3個byte?而不是2個。是utf-8字符集的原因?
誰知道??????
參考文獻:Oracle9i SQL Reference Release 2 (9.2)
..................................................用String的getBytes方法測試了一下.
結論是utf-8的中文字符占用3個字節,gbk的中文字符占用2個字節,iso-8859-1的中文字符被識別為占用2個字節,iso不支持中文字符的編碼,應該是都當成某個拉丁字母了.Oracle沒有關系,oracle只是負責存儲數據.
可以先用 select * from v$nls_parameters 看看oracle的字符集
下邊是測試的類:
import java.io.UnsupportedEncodingException;
public class TextEncoding {
/**
*
* @author:sunflower
* @date: 2007-1-24 上午10:09:40
* @todo: 調用的是String的自己的getBytes(encoding)方法,
* 使用指定的字符集將此 String 解碼為字節序列,并將結果存儲到一個新的字節數組中.
* @param content
* @param encode
* @return
*/
public static byte[] getBytes(String content,String charsetName)
throws UnsupportedEncodingException{
return content.getBytes(charsetName);
}
/**
*
* @author:sunflower
* @date: 2007-1-24 上午10:19:40
* @todo: 調用的是String的自己的getBytes()方法,
* 使用平臺默認的字符集將此 String 解碼為字節序列,并將結果存儲到一個新的字節數組中。
* @param content
* @return
*/
public static byte[] getBytes(String content){
return content.getBytes();
}
public static void main(String[]args){
String content="1e寶寶";
byte[] len;
try{
len=getBytes(content,"UTF-8");
System.out.println(" the byte array length is "+len.length);
len=getBytes(content,"GBK");
System.out.println(" the byte array length is "+len.length);
len=getBytes(content,"ISO-8859-1");
System.out.println(" the byte array length is "+len.length);
}catch(Exception e){
System.out.println("Can 't recognize");
}
// System.out.println("the content byte[] length is "+);
}
}
輸出 :
the byte array length is 8
the byte array length is 6
the byte array length is 4
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1492768