数据库 --单行函数

1,字符函数

  • lower()--转换为小写

     select lower(ename) from emp;
  • upper() --转换为大写

     select upper(ename) from emp;
  • concat() --字符串连接函数

     select concat('Mr.', ename) from emp;
  • substr() --字符串截取函数

     select substr('我是中国人', 3, 3) from dual;

    得到结果:中国人

  • length() --字符串长度函数

     select length('我是中国人,我爱中国') from dual;

    得到结果:10

  • replace() --字符替换函数

     select replace('说你行你就行!', '行', '不行') from dual;

    得到结果:说你不行你就不行!

  • instr() --字符查找函数

     select instr('说你行你就行!', '行') from dual;

    得到结果:2

  • trim() --去空格函数“_”

     select trim('我是中国人 ') from dual;

    得到结果:我是中国人(没有最后的空格)

     

2,数值函数

  • round() --四舍五入函数

    select round(3.54) from dual;

    得到结果:4

  • mod() -- 求余函数

     select mod(10, 3) from dual;

    得到结果:1

  • ceil() -- 取整(向上取整)

     select ceil(2.555) from dual;

    得到结果:3

  • floor() -- 取整(向下取整)

     select floor(2.555) from dual;

    得到结果:2

     

3,转换函数

  • to_char() -- 转换为字符串函数

     -- 获取当前的系统时间
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
  • to_number() --转换为数字函数

     
  • to_date() --转换为日期函数

     select to_date('2020-10-10', 'yyyy-MM-dd') from dual;

    得到结果:2020/10/10

     

4,通用函数

  • nvl(e1, e2) -- 空值判断函数

    判断e1是否为空值:如果有值,不变;如果为空值,替换为e2

  • nvl2(e1, e2, e3) -- 增强型空值判断函数

    判断e1是否为空值:如果有值,替换为e2;如果为空值,替换为e3

  • coalesce () --合并函数

     --求所有员工的总工资(基本+奖金)
     select ename, sal, comm, coalesce(sal+comm,sal+0) from emp;
  • case表达式

     -- 工资在1000以下的是菜鸟
     -- 1000-3000之间的是老鸟
     -- 3000以上的是大佬
     select empno,
        ename,
        (case
          when sal<1000 then '菜鸟'
          when sal>=1000 and sal<=3000 then '老鸟'
          when sal>3000 then '大佬'
          -- else '大佬'
          end) from emp;
  • decode(条件, 值1, 返回值1, 值2,返回值2, ... 值n, 返回值n, 缺省值) --条件判断函数

    decode(条件,值,返回值1,返回值2)--如果条件等于值,则返回返回值1,否则为返回值2

     select empno, 
        ename,
         decode(sal,3000,'老鸟','小鸟')
     from emp;

     

5,日期函数

  • months_between(date1, date2)

    1,用于计算date1date2之间有几个月。 如果date1在日历中比date2晚,那么months_between()就返回一个正数。 2,如果date1在日历中比date2早,那么months_between()就返回一个负数。 3,如果date1date2日期一样,那么months_between()就返回一个0。

     select months_between(sysdate,
     to_date('2020-8-9','yyyy-MM-dd')
     ) from dual;
  • add_months(date, n)

    函数返回在日期 date 基础上 n 个月后的日期值,如果 n 的值为负数则返回日期 date 基础上 n 个月前的日期值(date 减去 n 个月)。

    注意:由于每个月的天数不同,当 date 是一个月中的最后一天时,函数返回计算后该月的最后一天。例如,用 add_months() 计算 2020 年 3 月 31 日一个月前的日期,返回 2020 年 2 月 29 日。

     select add_months(sysdate,1) from dual;
  • next_day(date,char)

    指定时间的下一个星期几(由char指定)所在的日期

    date参数为日期型,

    char:为1~7或Monday/Mon~Sunday/

     select next_day(sysdate, 3) from dual;
  • last_day(date)

    返回指定日期对应月份的最后一天

     select last_day(sysdate) from dual;
posted @ 2021-03-12 09:51  nick-wgh  阅读(117)  评论(0)    收藏  举报