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

  • 相关阅读:
    QT4.7.1 + VS2008 + QT Designer开发流程心得
    SharePoint 2010 托管元数据Bug (跟邮件提醒功能相关.小bug,大问题)
    SharePoint 2010 技巧系列: 控制Ribbon菜单权限(SiteActions的例子)
    发布一个SharePoint 2010 工具(复制,移动文件和文件夹)
    SHarePoint 2010 技巧 列验证 (column Validation)
    SharePoint 2010系列: 教你如何创建Internet 站点一 (设计母版页)
    SharePoint2010 技巧系列:快速开发Ribbon
    SharePoint 2010 技巧: 限制People Picker搜索非站点集内的用户
    SharePoint 2010 技巧系列 启用文档库接收邮件功能
    SharePoint 2010 技巧系列: 文档管理的自动分发功能
  • 原文地址:https://www.cnblogs.com/yangang2013/p/5742899.html
Copyright © 2011-2022 走看看