oracle常用函数汇总(分享)

一、运算符
算术运算符:+ – * / 可以在select 语句中使用
连接运算符:|| select deptno|| dname from dept;
比较运算符:> >= = != < <= like between is null in
逻辑运算符:not and or
集合运算符: intersect ,union, union all, minus
要求:对应集合的列数和数据类型相同
     查询中不能包含long 列
     列的标签是第一个集合的标签
     使用order by时,必须使用位置序号,不能使用列名

例:集合运算符的使用:

复制代码 代码如下:

intersect ,union, union all, minus

select * from emp intersect select * from emp where deptno=10 ;

select * from emp minus select * from emp where deptno=10;

select * from emp where deptno=10 union select * from emp where deptno in (10,20); –不包括重复行

select * from emp where deptno=10 union all select * from emp where deptno in (10,20); –包括重复行

二.oracle日期时间函数大全
   to_date格式(以时间:2007-11-02   13:45:25为例)

        year:     

        yy two digits 两位年                显示值:07

        yyy three digits 三位年                显示值:007

        yyyy four digits 四位年                显示值:2007

        month:     

        mm    number     两位月              显示值:11

        mon    abbreviated 字符集表示          显示值:11月,若是英文版,显示nov    

        month spelled out 字符集表示          显示值:11月,若是英文版,显示november

        day:     

        dd    number         当月第几天        显示值:02

        ddd    number         当年第几天        显示值:02

        dy    abbreviated 当周第几天简写    显示值:星期五,若是英文版,显示fri

        day    spelled out   当周第几天全写    显示值:星期五,若是英文版,显示friday       

        ddspth spelled out, ordinal twelfth

              hour:

              hh    two digits 12小时进制            显示值:01

              hh24 two digits 24小时进制            显示值:13

              minute:

              mi    two digits 60进制                显示值:45

              second:

              ss    two digits 60进制                显示值:25

              其它

              q     digit         季度                  显示值:4

              ww    digit         当年第几周            显示值:44

              w    digit          当月第几周            显示值:1

        24小时格式下时间范围为: 0:00:00 – 23:59:59….     

        12小时格式下时间范围为: 1:00:00 – 12:59:59 ….

1. 日期和字符转换函数用法(to_date,to_char)
        

select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’) as nowtime from dual;   //日期转化为字符串  

select to_char(sysdate,’yyyy’) as nowyear   from dual;   //获取时间的年  

select to_char(sysdate,’mm’)    as nowmonth from dual;   //获取时间的月  

select to_char(sysdate,’dd’)    as nowday    from dual;   //获取时间的日  

select to_char(sysdate,’hh24′) as nowhour   from dual;   //获取时间的时  

select to_char(sysdate,’mi’)    as nowminute from dual;   //获取时间的分  

select to_char(sysdate,’ss’)    as nowsecond from dual;   //获取时间的秒

select to_date(‘2004-05-07 13:23:44′,’yyyy-mm-dd hh24:mi:ss’)    from dual//

2. select to_char( to_date(222,’j’),’jsp’) from dual     

    显示two hundred twenty-two  

3.求某天是星期几     

   select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’) from dual;     

   星期一     

   select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;     

   monday     

   设置日期语言     

   alter session set nls_date_language=’american’;     

   也可以这样     

   to_date (‘2002-08-26’, ‘yyyy-mm-dd’, ‘nls_date_language = american’)     

4. 两个日期间的天数     

    select floor(sysdate – to_date(‘20020405′,’yyyymmdd’)) from dual;  

5. 时间为null的用法     

   select id, active_date from table1     

   union     

   select 1, to_date(null) from dual;      

   注意要用to_date(null)     

6.月份差  

   a_date between to_date(‘20011201′,’yyyymmdd’) and to_date(‘20011231′,’yyyymmdd’)     

   那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。     

   所以,当时间需要精确的时候,觉得to_char还是必要的

7. 日期格式冲突问题     

    输入的格式要看你安装的oracle字符集的类型, 比如: us7ascii, date格式的类型就是: ’01-jan-01′     

    alter system set nls_date_language = american     

    alter session set nls_date_language = american     

    或者在to_date中写     

    select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;     

    注意我这只是举了nls_date_language,当然还有很多,     

    可查看     

    select * from nls_session_parameters     

    select * from v$nls_parameters     


8.     


复制代码 代码如下:

   select count(*)     

   from ( select rownum-1 rnum     

       from all_objects     

       where rownum <= to_date(‘2002-02-28′,’yyyy-mm-dd’) – to_date(‘2002-     

       02-01′,’yyyy-mm-dd’)+1     

      )     

   where to_char( to_date(‘2002-02-01′,’yyyy-mm-dd’)+rnum-1, ‘d’ )     

        not in ( ‘1’, ‘7’ )     

   查找2002-02-28至2002-02-01间除星期一和七的天数     

   在前后分别调用dbms_utility.get_time, 让后将结果相减(得到的是1/100秒, 而不是毫秒).     

9. 查找月份  


复制代码 代码如下:

    select months_between(to_date(’01-31-1999′,’mm-dd-yyyy’),to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;     

    1     

   select months_between(to_date(’02-01-1999′,’mm-dd-yyyy’),to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;     

    1.03225806451613

10. next_day的用法     


复制代码 代码如下:

    next_day(date, day)     

    monday-sunday, for format code day     

    mon-sun, for format code dy     

    1-7, for format code d    

11     

   select to_char(sysdate,’hh:mi:ss’) time from all_objects     

   注意:第一条记录的time 与最后一行是一样的     

   可以建立一个函数来处理这个问题     


复制代码 代码如下:

   create or replace function sys_date return date is     

   begin     

   return sysdate;     

   end;     

   select to_char(sys_date,’hh:mi:ss’) from all_objects;  

12.获得小时数     

     extract()找出日期或间隔值的字段值


复制代码 代码如下:

    select extract(hour from timestamp ‘2001-02-16 2:38:40’) from offer     

    sql> select sysdate ,to_char(sysdate,’hh’) from dual;     

    sysdate to_char(sysdate,’hh’)     

    ——————– ———————     

    2003-10-13 19:35:21 07     

    sql> select sysdate ,to_char(sysdate,’hh24′) from dual;     

    sysdate to_char(sysdate,’hh24′)     

    ——————– ———————–     

    2003-10-13 19:35:21 19     

13.年月日的处理     


复制代码 代码如下:

   select older_date,     

       newer_date,     

       years,     

       months,     

       abs(     

        trunc(     

         newer_date-     

         add_months( older_date,years*12+months )     

        )     

       ) days

   from ( select     

        trunc(months_between( newer_date, older_date )/12) years,     

        mod(trunc(months_between( newer_date, older_date )),12 ) months,     

        newer_date,     

        older_date     

        from (

              select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date     

              from emp

             )     

      )     

14.处理月份天数不定的办法     

   select to_char(add_months(last_day(sysdate) +1, -2), ‘yyyymmdd’),last_day(sysdate) from dual     


16.找出今年的天数     

   select add_months(trunc(sysdate,’year’), 12) – trunc(sysdate,’year’) from dual     

   闰年的处理方法     

   to_char( last_day( to_date(’02’    | | :year,’mmyyyy’) ), ‘dd’ )     

   如果是28就不是闰年     


17.yyyy与rrrr的区别     


复制代码 代码如下:

   ‘yyyy99 to_c     

   ——- —-     

   yyyy 99 0099     

   rrrr 99 1999     

   yyyy 01 0001     

   rrrr 01 2001     

18.不同时区的处理     

   select to_char( new_time( sysdate, ‘gmt’,’est’), ‘dd/mm/yyyy hh:mi:ss’) ,sysdate     

   from dual;   

19.5秒钟一个间隔  


复制代码 代码如下:

   select to_date(floor(to_char(sysdate,’sssss’)/300) * 300,’sssss’) ,to_char(sysdate,’sssss’)     

   from dual     

   2002-11-1 9:55:00 35786     

   sssss表示5位秒数    

  


20.一年的第几天     

   select to_char(sysdate,’ddd’),sysdate from dual

   310 2002-11-6 10:03:51     

21.计算小时,分,秒,毫秒 


复制代码 代码如下:

     select     

     days,     

     a,     

     trunc(a*24) hours,     

     trunc(a*24*60 – 60*trunc(a*24)) minutes,     

     trunc(a*24*60*60 – 60*trunc(a*24*60)) seconds,     

     trunc(a*24*60*60*100 – 100*trunc(a*24*60*60)) mseconds     

    from     

    (     

     select     

     trunc(sysdate) days,     

     sysdate – trunc(sysdate) a     

     from dual     

   )     

   select * from tabname     

   order by decode(mode,’fifo’,1,-1)*to_char(rq,’yyyymmddhh24miss’);     

   //     

   floor((date2-date1) /365) 作为年     

   floor((date2-date1, 365) /30) 作为月     

   d(mod(date2-date1, 365), 30)作为日.

23.next_day函数      返回下个星期的日期,day为1-7或星期日-星期六,1表示星期日

   next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。     

   1 2 3 4 5 6 7     

   日 一 二 三 四 五 六   

   —————————————————————

   select    (sysdate-to_date(‘2003-12-03 12:55:45′,’yyyy-mm-dd hh24:mi:ss’))*24*60*60 from ddual

   日期 返回的是天 然后 转换为ss

24,round[舍入到最接近的日期](day:舍入到最接近的星期日)

   select sysdate s1,

   round(sysdate) s2 ,

   round(sysdate,’year’) year,

   round(sysdate,’month’) month ,

   round(sysdate,’day’) day from dual

25,trunc[截断到最接近的日期,单位为天] ,返回的是日期类型

   select sysdate s1,                    

     trunc(sysdate) s2,                 //返回当前日期,无时分秒

     trunc(sysdate,’year’) year,        //返回当前年的1月1日,无时分秒

     trunc(sysdate,’month’) month ,     //返回当前月的1日,无时分秒

     trunc(sysdate,’day’) day           //返回当前星期的星期天,无时分秒

   from dual

26,返回日期列表中最晚日期

   select greatest(’01-1月-04′,’04-1月-04′,’10-2月-04′) from dual

27.计算时间差

     注:oracle时间差是以天数为单位,所以换算成年月,日

      select floor(to_number(sysdate-to_date(‘2007-11-02 15:55:03′,’yyyy-mm-dd hh24:mi:ss’))/365) as spanyears from dual        //时间差-年

      select ceil(moths_between(sysdate-to_date(‘2007-11-02 15:55:03′,’yyyy-mm-dd hh24:mi:ss’))) as spanmonths from dual        //时间差-月

      select floor(to_number(sysdate-to_date(‘2007-11-02 15:55:03′,’yyyy-mm-dd hh24:mi:ss’))) as spandays from dual             //时间差-天

      select floor(to_number(sysdate-to_date(‘2007-11-02 15:55:03′,’yyyy-mm-dd hh24:mi:ss’))*24) as spanhours from dual         //时间差-时

      select floor(to_number(sysdate-to_date(‘2007-11-02 15:55:03′,’yyyy-mm-dd hh24:mi:ss’))*24*60) as spanminutes from dual    //时间差-分

      select floor(to_number(sysdate-to_date(‘2007-11-02 15:55:03′,’yyyy-mm-dd hh24:mi:ss’))*24*60*60) as spanseconds from dual //时间差-秒

28.更新时间
     注:oracle时间加减是以天数为单位,设改变量为n,所以换算成年月,日

     select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n*365,’yyyy-mm-dd hh24:mi:ss’) as newtime from dual        //改变时间-年

     select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’),add_months(sysdate,n) as newtime from dual                                 //改变时间-月

     select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n,’yyyy-mm-dd hh24:mi:ss’) as newtime from dual            //改变时间-日

     select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n/24,’yyyy-mm-dd hh24:mi:ss’) as newtime from dual         //改变时间-时

     select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n/24/60,’yyyy-mm-dd hh24:mi:ss’) as newtime from dual      //改变时间-分

     select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n/24/60/60,’yyyy-mm-dd hh24:mi:ss’) as newtime from dual   //改变时间-秒

29.查找月的第一天,最后一天
     select trunc(trunc(sysdate, ‘month’) – 1, ‘month’) first_day_last_month,

       trunc(sysdate, ‘month’) – 1 / 86400 last_day_last_month,

       trunc(sysdate, ‘month’) first_day_cur_month,

       last_day(trunc(sysdate, ‘month’)) + 1 – 1 / 86400 last_day_cur_month

   from dual;

三. 字符函数(可用于字面字符或数据库列)
1,字符串截取
   select substr(‘abcdef’,1,3) from dual

2,查找子串位置
   select instr(‘abcfdgfdhd’,’fd’) from dual

3,字符串连接
   select ‘hello’||’hello world’ from dual;

4, 1)去掉字符串中的空格
    select ltrim(‘ abc’) s1,

    rtrim(‘zhang ‘) s2,

    trim(‘ zhang ‘) s3 from dual

  
2)去掉前导和后缀
    select trim(leading 9 from 9998767999) s1,

    trim(trailing 9 from 9998767999) s2,

    trim(9 from 9998767999) s3 from dual;

5,返回字符串首字母的ascii值
   select ascii(‘a’) from dual

6,返回ascii值对应的字母
   select chr(97) from dual

7,计算字符串长度
   select length(‘abcdef’) from dual

8,initcap(首字母变大写) ,lower(变小写),upper(变大写)
   select lower(‘abc’) s1, 

       upper(‘def’) s2,

       initcap(‘efg’) s3

   from dual;

9,replace

   select replace(‘abc’,’b’,’xy’) from dual;

10,translate

   select translate(‘abc’,’b’,’xx’) from dual; — x是1位

11,lpad [左添充] rpad [右填充](用于控制输出格式)
   select lpad(‘func’,15,’=’) s1, rpad(‘func’,15,’-‘) s2 from dual;

   select lpad(dname,14,’=’) from dept;

12, decode[实现if ..then 逻辑]   注:第一个是表达式,最后一个是不满足任何一个条件的值

   select deptno,decode(deptno,10,’1′,20,’2′,30,’3′,’其他’) from dept;

   例:

   select seed,account_name,decode(seed,111,1000,200,2000,0) from t_userinfo//如果seed为111,则取1000;为200,取2000;其它取0

   select seed,account_name,decode(sign(seed-111),1,’big seed’,-1,’little seed’,’equal seed’) from t_userinfo//如果seed>111,则显示大;为200,则显示小;其它则显示相等

13 case[实现switch ..case 逻辑]

复制代码 代码如下:

    select case x-field

         when x-field < 40 then ‘x-field 小于 40’

         when x-field < 50 then ‘x-field 小于 50’

         when x-field < 60 then ‘x-field 小于 60’

         else ‘unbeknown’

        end

   from dual

  

注:case语句在处理类似问题就显得非常灵活。当只是需要匹配少量数值时,用decode更为简洁。

四.数字函数
1,取整函数(ceil 向上取整,floor 向下取整)
   select ceil(66.6) n1,floor(66.6) n2 from dual;

2, 取幂(power) 和 求平方根(sqrt)
   select power(3,2) n1,sqrt(9) n2 from dual;

3,求余
   select mod(9,5) from dual;

4,返回固定小数位数 (round:四舍五入,trunc:直接截断)
   select round(66.667,2) n1,trunc(66.667,2) n2 from dual;

5,返回值的符号(正数返回为1,负数为-1)
   select sign(-32),sign(293) from dual;

五.转换函数


1,to_char()[将日期和数字类型转换成字符类型]
   1) select to_char(sysdate) s1,

        to_char(sysdate,’yyyy-mm-dd’) s2,

        to_char(sysdate,’yyyy’) s3,

        to_char(sysdate,’yyyy-mm-dd hh12:mi:ss’) s4,

        to_char(sysdate, ‘hh24:mi:ss’) s5,

        to_char(sysdate,’day’) s6

    from dual;

   2) select sal,to_char(sal,’$99999′) n1,to_char(sal,’$99,999′) n2 from emp

2, to_date()[将字符类型转换为日期类型]
    insert into emp(empno,hiredate) values(8000,to_date(‘2004-10-10′,’yyyy-mm-dd’));

3, to_number() 转换为数字类型
    select to_number(to_char(sysdate,’hh12′)) from dual; //以数字显示的小时数

六.其他函数
  
1.user:

    返回登录的用户名称

    select user from dual;

  
2.vsize:
    返回表达式所需的字节数

    select vsize(‘hello’) from dual;

  
3.nvl(ex1,ex2):  
    ex1值为空则返回ex2,否则返回该值本身ex1(常用)

    例:如果雇员没有佣金,将显示0,否则显示佣金

    select comm,nvl(comm,0) from emp;

  
4.nullif(ex1,ex2):
    值相等返空,否则返回第一个值

    例:如果工资和佣金相等,则显示空,否则显示工资

    select nullif(sal,comm),sal,comm from emp;

  
5.coalesce:  

    返回列表中第一个非空表达式

    select comm,sal,coalesce(comm,sal,sal*10) from emp;

  
6.nvl2(ex1,ex2,ex3) :
    如果ex1不为空,显示ex2,否则显示ex3

    如:查看有佣金的雇员姓名以及他们的佣金

    select nvl2(comm,ename,’) as havecommname,comm from emp;

  


七.分组函数
max min avg count sum


1,整个结果集是一个组

   1) 求部门30 的最高工资,最低工资,平均工资,总人数,有工作的人数,工种数量及工资总和


复制代码 代码如下:

     select max(ename),max(sal),

     min(ename),min(sal),

     avg(sal),

     count(*) ,count(job),count(distinct(job)) ,

     sum(sal) from emp where deptno=30;

2, 带group by 和 having 的分组

   1)按部门分组求最高工资,最低工资,总人数,有工作的人数,工种数量及工资总和


复制代码 代码如下:    

    select deptno, max(ename),max(sal),

    min(ename),min(sal),

    avg(sal),

    count(*) ,count(job),count(distinct(job)) ,

    sum(sal) from emp group by deptno;

  

   2)部门30的最高工资,最低工资,总人数,有工作的人数,工种数量及工资总和


复制代码 代码如下:    

    select deptno, max(ename),max(sal),

    min(ename),min(sal),

    avg(sal),

    count(*) ,count(job),count(distinct(job)) ,

    sum(sal) from emp group by deptno having deptno=30;

  


3, stddev 返回一组值的标准偏差

    select deptno,stddev(sal) from emp group by deptno;

    variance 返回一组值的方差差

    select deptno,variance(sal) from emp group by deptno;

4, 带有rollup和cube操作符的group by

    rollup 按分组的第一个列进行统计和最后的小计

    cube 按分组的所有列的进行统计和最后的小计

    select deptno,job ,sum(sal) from emp group by deptno,job;

    select deptno,job ,sum(sal) from emp group by rollup(deptno,job);

    cube 产生组内所有列的统计和最后的小计

    select deptno,job ,sum(sal) from emp group by cube(deptno,job);



八、临时表


   只在会话期间或在事务处理期间存在的表.

   临时表在插入数据时,动态分配空间


复制代码 代码如下:

   create global temporary table temp_dept

   (dno number,

   dname varchar2(10))

   on commit delete rows;

   insert into temp_dept values(10,’abc’);

   commit;

   select * from temp_dept; –无数据显示,数据自动清除

   on commit preserve rows:在会话期间表一直可以存在(保留数据)

   on commit delete rows:事务结束清除数据(在事务结束时自动删除表的数据)

除法:


复制代码 代码如下:

select mod(65,50) from dual     –取余

select  trunc( 65/33) from dual  — 取整 trunc (1.9) = 1

select ceil(65/60) from dual          — 取整 ceil(1.1) = 2

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐