oracel开发的一些基本函数,translate,instr,instrb等

来源:互联网 发布:js实现图片沿轨迹运动 编辑:程序博客网 时间:2024/05/29 03:09
select rtrim('   56   ') as ab from dual; --去除右边的空格
select ltrim('   56   ') as ab from dual; --去除左边的空格
select trim('   56   ') as ab from dual;  --去除左右两边的空格
select instr('abc','a',1,2) from dual;   --从(参数3)的位置开始找出(参数2)在(参数1)中的第参数4次出现的位置,(也就是在abc中找到a的第二次出现的位置),该语句返回结果是0
select instr('abaca','a',-1,3) from dual; --(参数3)可以是负数,这时候是从右向左开始检索,该语句返回结果是1
select instrb('甲骨文','骨',1) from dual; --和instr函数一样,不过返回的是字节的位置,该语句返回3,因为一个汉字两个字节
select instr('甲骨文') from dual;  --返回字符串长度,结果是3
select lengthb('甲骨文') from dual; --返回字节长度,结果是6,PS:为汉字的时候结果有区别
select rpad('test',5,'o') from dual;--返回(参数2)的字节数,如果(参数1)不够则用(参数3)从右边填充,该语句结果是testo,如果参数1够长,任然保持参数1的长度
select lpad('test',5,'o') from dual;--返回(参数2)的字节数,如果(参数1)不够则用(参数3)从左边填充,该语句结果是otest
select translate('robot','roB','*') "License" from dual; --将参数2和参数3的字符一一对应,也就是r对*,然后在参数1中找出参数2的字符,用参数3对应的替代,再返回结果,

该语句返回*bt,如果参数2中有而参数3中没有,则将参数2中有在参数1中也有的剔除,如上就剔除了o,由于oracle区分大小写,所以b留下来了。


select translate('robot',substr('robot',1,length('robot') - 1),rpad('*',length('robot'),'*')) "License" from dual; --综合一下,返回结果****t,可以用于保密姓名,又可以作为参考信息


select substr('abcdefg',-4,2) from dual; --截取参数1中的字符,如果参数2是负数则从后面开始截取,为正数就从前面开始截取,如果正数参数3默认是一位,如果是负数,默认是负数的的位置开始截取返回结果是degf
select substr('abcd',1,2) from dual; --返回结果是ab
select replace('robot','o','a') from dual; --替代函数
select upper('robot') as ab from dual;  --转换大写函数
select lower('ROBOT') as ab from dual; --转换小写函数


--循环处理
declare i number :=1;
begin
while i<=5 loop
  i:=i+1;
  dbms_output.put_line(i); 
end loop;
end;
0 0
原创粉丝点击