1、LENGTH( string1 ) 2、LENGTHB( string1 ) 在oracle中,这两个函数都有差不多意思,最大的区别在于:length 求得是字符长度,lengthb求得是字节长度。 在了解这些之后,看些例子就明白了
-- 查询姓名
select colName1 as 编号, colName2 as 姓名 from tableName;
-- 查询姓名长度为2的
select colName1 as 编号, colName2 as 姓名 from tableName where length(colName2) = 2;
-- 查询字节为6 的,也就是三位
select colName1 as 编号, colName2 as 姓名 from tableName where lengthb(colName2) = 6;
我们看一下有哪些情况
-- NULL对象 LENGTH(NULL) Result: NULL -- 空值字符窜 LENGTH('') Result: NULL -- 有一个字节的空 LENGTH(' ') Result: 1 -- LENGTH('Tech on the Net') Result: 15 -- 注意后面多了一个空格 LENGTH('Tech on the Net ') Result: 16 -- 汉字,一个汉字一位 LENGTH('张三') Result: 2
LENGTHB(NULL) Result: NULL (single-byte character set) LENGTHB('') Result: NULL (single-byte character set) LENGTHB(' ') Result: 1 (single-byte character set) LENGTHB('TechOnTheNet.com') Result: 16 (single-byte character set) LENGTHB('TechOnTheNet.com ') Result: 17 (single-byte character set)