zoukankan      html  css  js  c++  java
  • Oracle的基本学习(三)—函数

    一、字符函数

    image

    1.大小写控制函数

    --lower:使字母变为小写--
    --upper:使字母变为大写--
    --initcap:使字符的第一个字母变为大写--
    select 
      lower('ABC'),
      upper('sql'),
      initcap('HeLlo SQL')
     from dual;

    image

    select employee_id, department_id,last_name, salary
      from employees
     where lower(last_name)='king';

    image

    2.字符控制函数

    concat(str1,str2)

         连接两个字符串。

    substr(str,index,n)

         截取字符串,从index开始(sql字符串下标第一个为1),截取n个长度。

    length(str)

         获取str的长度。

    instr(str1,str2)

         str2在str1首次出现的索引,如果不存在返回0。

    lpad(str1,len,str2)

         设置str1长度为len,如果长度不够在左边用str2补齐。

    rpad(str1,len,str2)

         设置str1长度为len,如果长度不够在右边用str2补齐。

    trim(str1,from str2)

         去掉str2中的st1,仅仅是首部和尾部的。

    replace(str,str1,str2)

         把str中的str1替换成str2,全部都替换。

    select 
     concat('hello','sql'),
     substr('hellosql',2,4),
     instr('HelloWorld','o'),
     length('hello sql') 
    from dual;

    image

    select employee_id,last_name, salary,lpad(salary,10,'*')
      from employees
     where department_id=80;

    image

    --trim:仅仅去掉首位和尾部的--
    --replace:替换所有的--
    select 
     trim('A' from 'AABBAACCAA'),
     replace('AABBAACCAA','A','M')
    from dual;

    image

    二、数字函数

    round: 四舍五入函数。

    trunc: 截断函数。

    mod: 求余函数。

    --round:四舍五入--
    select 
       round(435.45,1),
       round(435.45),
       round(435.45,-1)
    from dual;

    image

    --trunc:截断--
    select 
       trunc(435.45,1),
       trunc(435.45),
       trunc(435.45,-1)
    from dual;

    image

    三、日期相关函数

    to_char(date,'format_model')

         对日期的转换。

     select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') 
     from dual;

    image

    四、通用函数

    nvl函数

    格式: nvl(E1,E2)

    解释: 如果E1为NULL,则函数返回E2,否则就返回E1。

    nvl2函数

    格式: nvl2(E1,E2,E3)

    解释: 如果E1为NULL,则函数返回E3,若E1不为null,则返回E2。

    nullif函数

    格式: nullif(exp1,exp2)

    解释: 如果两个表达式不相等,NULLIF 返回第一个exp1的值。如果两个表达式相等,NULLIF 返回空值NULL。

    coalesce函数

    格式: coalesce(exp1,exp2,,,,,)

    解释: 依次参考各参数表达式,遇到非null值即停止并返回该值。如果所有的表达式都是空值,最终将返回一个空值。

    case表达式

    SELECT last_name,
           job_id,
           salary,
           department_id,
           CASE department_id
             WHEN 10 THEN
              10 * salary
             WHEN 20 THEN
              20 * salary
             WHEN 30 THEN
              30 * salary
             ELSE
              salary
           END new_salary
      FROM employees
     WHERE department_id IN (10, 20, 30);

    image

    decode函数

    SELECT last_name,
           job_id,
           salary,
           department_id,
           DECODE(department_id,
                  10,10 * salary,
                  20,20 * salary,
                  30,40 * salary,
                  salary) new_salary
      FROM employees
     WHERE department_id IN (10, 20, 30);

    image

  • 相关阅读:
    bzoj 4446: [Scoi2015]小凸玩密室【树形dp】
    bzoj 4403: 序列统计【lucas+组合数学】
    bzoj 3745: [Coci2015]Norma【分治】
    bzoj 3232: 圈地游戏【分数规划+最小割】
    Codeforces 1000 (A~E)
    bzoj 4753: [Jsoi2016]最佳团体【01分数规划+二分+树上背包】
    bzoj 3872: [Poi2014]Ant colony【树形dp+二分】
    bzoj 2067: [Poi2004]SZN【贪心+二分+树形dp】
    洛谷 P1314 聪明的质监员【二分+前缀和】
    bzoj 4622: [NOI 2003] 智破连环阵【dfs+匈牙利算法】
  • 原文地址:https://www.cnblogs.com/yangang2013/p/5742899.html
Copyright © 2011-2022 走看看