oracle函数


一、大小写函数

lower():全部小写
upper():全部大写
initcap():首字母大写

--小写
select lower ('HAPPY') "Lowercase" from dual

--大写
select upper('last name'"Uppercase" from dual

--首字母大写
select initcap ('the socp') "Capitals" from dual


二、字符控制函数

① Concat--连接字符串

select concat ('happy','boy') from dual

② Substr

 substr('要截取的字符串',起始位置)

select substr ('HappyBoy',2,3) from dual

③ length('字符串'):字符个数统计

    lengthb('字符串'):字节个数统计

select length('快乐') 字符数,lengthb('快乐') as 字节数 from dual

④ instr('大字符串','小字符串') 返回小字符串在大字符串中出现的位置

select instrb('corporate floor','or',5,2) "Instring in bytes" from dual

⑤ lpad()和rpad()

lpad则与rpad相反

select rpad('Happy',10,'*') from dual

⑥ trim()去除带有“a”的字符

select trim('a' from 'ahappy') from dual


三、日期函数

① months_between:两个日期相差的月数

② add_months:向指定日期中加上若干月数

--两个日期相差的月数

select months_between
(
to_date('02-02-1995','MM-DD-YYYY'),
to_date('01-01-1995','MM-DD-YYYY')
) "Months"
from dual

--向指定日期中加上若干月数

select to_char(add_months(HIREDATE,1),'DD-MON-YYYY') "Next month"
from  emp
where ENAME='WARD';

-----日期相减

---两个日期间的天数
select floor (sysdate - to_date('20020405','yyyymmdd')) from dual;

---两个日期相差的月数
select months_between(sysdate,to_date('20111204','yyyymmdd')) from dual


四、转换函数

隐式转换:

--隐式转换
select * from emp
where hiredate='17-12月-80'

显示转换:

--显式转换
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;


五、数字函数

① round():四舍五入

select round(12.45,1) from dual;

② trunc:截断

select trunc(15.79,1) "Truncate" from dual


六、通用函数

nvl和nvl2

--通用函数
select sal*12工资,comm 奖金,sal*12+nvl(comm,0) from emp

select sal*12工资,comm 奖金,sal*12+nvl2(comm,comm,0) from emp


七、decode函数

 select ename,empno,
      decode (ename,'SMITH',1,
      'ALLEN',2,
      'WARD',3,
      'JONES',4) "Location"
      from emp
      where empno<7600
      order by empno,"Location"

原文地址:https://www.cnblogs.com/WJ-163/p/5810961.html