oracle常用语的几个函数

--instr在一个字符串中搜索指定的字符,返回发现指定的字符的位置;第一个字符的位置为1,不是0
select instr('raojing','o') from dual;  --result: 3

--length返回字符串的长度
select length('test') from dual;  --result: 4

--REPLACE(string,s1,s2)
--string 希望被替换的字符或变量  s1 被替换的字符串 s2 要替换的字符串
select replace('he love you','he','I') from dual;

--SUBSTR(string,start,count)取子字符串.从start位置开始,取count个,字符串第一个字符的位置为1,子字符串包含start位置
select substr('raojing',1) from dual;    --result: raojing
select substr('raojing',4,2) from dual;  --result: ji

--NVL(expr1, expr2)->expr1为NULL,返回expr2;不为NULL,返回expr1。注意两者的类型要一致  
select nvl(null,2) from dual;  --result:2
select nvl(1,2) from dual; --result:1

--expr1不为NULL,返回expr2;为NULL,返回expr3。expr2和expr3类型不同的话,expr3会转换为expr2的类型
select nvl2(1,2,3) from dual;   --result :2
select nvl2(null,2,3) from dual;  --result:3

--NULLIF (expr1, expr2) ->相等返回NULL,不等返回expr1
SELECT  nullif(2,3) from dual;  --result:2

--RPAD和LPAD(粘贴字符)不够字符则用*或+来填满
select lpad(rpad('gao',5,'*'),10,'+')from dual;

--sign取数字n的符号,大于0返回1,小于0返回-1,等于0返回0
select sign(123),sign(-1),sign(0) from dual;

--add_months增加或减去月份
select to_char(add_months(to_date(201205,'yyyymm'),-4),'yyyymm') from dual;
select to_char(add_months(to_date(201205,'yyyymm'),10),'yyyymm') from dual;

--返回日期的最后一天
select last_day(to_date(201203,'yyyymm')) from dual;
select last_day(sysdate) from dual;

select months_between(to_date('2000.05.20','yyyy.mm.dd'),to_date('2005.05.20','yyyy.mm.dd')) mon_between from dual;

--返回当前用户
select user from dual;

猜你喜欢

转载自aniyo.iteye.com/blog/1522655