zoukankan      html  css  js  c++  java
  • Oracle函数

    Oracle将函数大致分为单行函数,聚合函数和分析函数。

    单行函数分为字符函数,日期函数,转换函数,数字函数,通用函数,decode函数

    一.字符函数

        03.length()和lengthb()

      --length('字符串'):字符个数统计
       -- lengthb('字符串'):字节个数统计
    select length('呵呵') 字符数,lengthb('呵呵') as 字节数 from dual;

    效果:

        04.instr()

    --instr('大字符串','小字符串')返回小字符串在大字符串中出现的位置
    select instr('happy hehe','he',2,2) "Instring" from dual;

    select instr('happy hehe','he',-2,2) "Reversed Instring" from dual;

    效果:

    select instr('happy hehe','he',2,2) "Instring in bytes" from dual;

    效果:

        05.lpad()和rpad()

    --lpad()和rpad()
    select lpad('happy',10,'*') from dual;

    效果:

    二.日期函数

    1)日期函数

         01.两个日期相差的月数

    select MONTHS_BETWEEN
    (TO_DATE('02-02-1995','MM-DD-YYYY'),
    TO_DATE('01-01-1995','MM-DD-YYYY')) "Months"
    from dual;

    效果:

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

    --向指定日期中加上若干月数
    select TO_CHAR(ADD_MONTHS(hiredate,1),'DD-MON-YYYY') "Next month" 
    from emp
    where ENAME='JONES';

    效果:

    2)日期相减

        01.两个日期间的天数

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

    效果:

    三。转换函数

    1)隐式转换

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

    效果:

    2)显示转换

       01.to_char()对日期的转换

    --显式函数
       --01.to_char()对日期的转换
       select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

    效果:

       02.to_char()对数字的转换

    --02.to_char()对数字的转换
       select to_char(sal,'L9,999.99') 
       from emp;

    效果:

    四.数字函数

        01.Round()

    --数字函数
      --01.Round()四舍五入
      select round(12.45,1) from dual;

    效果:

        02.trunc()截断

     --02.trunc()截断
      select trunc(15.19,1) "Truncate" from dual;

    效果:

     五.通用函数

    nvl和nvl2滤空函数

       01.nvl滤空函数

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

    效果:

       02.nvl2滤空函数

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

    效果:

     六.decode函数

    复制代码
    复制代码
    --decode函数
      select ename,empno,
          decode (ename,'SMITH',1,
          'ALLEN',2,
          'WARD',3,
          'JONES',4) "Location"
          from emp
          where empno<7600
          order by empno,"Location"
    复制代码
    复制代码

    效果:

  • 相关阅读:
    数据结构:图(Graph)
    数据结构:图(Graph)
    sql中的左右关联、全关联、自关联
    sql中的左右关联、全关联、自关联
    Mathematical Functions (TransactSQL)
    Mathematical Functions (TransactSQL)
    数据库中SQL实现某列的乘积(SqlSERVER)
    数据库中SQL实现某列的乘积(SqlSERVER)
    devexpress表格控件gridcontrol设置隔行变色、焦点行颜色、设置(改变)显示值、固定列不移动(附源码)
    devexpress表格控件gridcontrol设置隔行变色、焦点行颜色、设置(改变)显示值、固定列不移动(附源码)
  • 原文地址:https://www.cnblogs.com/liang67732116/p/5811452.html
Copyright © 2011-2022 走看看