Oracle中查询条件

来源:互联网 发布:动态截图软件 编辑:程序博客网 时间:2024/05/18 00:45

 * 在姓名表中查询:
1 . 姓陈
2 . 姓名是三个字的姓名
3 . 性别为女
4 . 在2000年后出生
select count((t.last_name || t.first_name)) as countNumber,(t.last_name || t.first_name) as vipName,t.gender
  from name_info_t t
 where ((length(last_name) + length(first_name)) > 2 and
       (length(last_name) + length(first_name)) < 4 and
       t.birthday > to_date('2000-01-01', 'yyyy-MM-dd') and
       t.last_name like '%陈%' and t.gender <> '1')--0:女   1:男
 group by (t.last_name || t.first_name), t.gender;

 * lengthb(string)计算string所占的字节长度:返回字符串的长度,单位是字节
   length(string)计算string所占的字符长度:返回字符串的长度,单位是字符

用length('string')=lengthb('string')判断字符串是否含有中文。
对于单字节字符,LENGTHB和LENGTH是一样的。
一个汉字在Oracle数据库里占多少字节跟数据库的字符集有关,UTF8时,长度为三。
 select lengthb('飘') from dual;
 select length('飘') from dual;  
可查询汉字在Oracle数据库里占多少字节
 * length(123)=3,是计算123的长度length('abc')=3,是计算'abc'的长度length(123abc)和length(abc)中,函数将其当做是变量,因为它既不是有效的数字,也不是有效的字符串(如'asdf',带引号的),所以就会出现"标识符无效"等错误。

 * 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 返回内部表示的字节的数目。
select length('asdfg测试') bytesLengthIs from dual ;
--7
select lengthb('asdfg') bytesLengthIs from dual ;
select vsize('asdfg') bytesLengthIs from dual ;
--5
select lengthb('asdfg测试') bytesLengthIs from dual ;
select vsize('asdfg测试') bytesLengthIs from dual ;
--11
select lengthc('asdfg测试') bytesLengthIs from dual ;
--7
则在utf-8的字符集下
lengthb=vsize
lengthc=length
参考文献:Oracle9i SQL Reference Release 2 (9.2)

希望对你有帮助,祝你有一个好心情,加油!

若有错误、不全、可优化的点,欢迎纠正与补充!

原创粉丝点击