oracle 10g学习5

一、单值函数

位置:可以出现在select、where、order by字句中
    单值函数:
        字符函数
        数字函数
        日期函数
        转换函数

1,字符函数

(1)LOWER:大写转小写
   select last_name,salary
   from s_emp
   where lower(last_name)='chang'   
(2)UPPER:小写转大写
      select upper('hello') from dual;
(3)INITCAP:将每一个单词首字母大写,其他小写
      select initcap('hello,world,hello,oracle!')
      from dual;

(4)CONCAT:作用与||相同,用于连接字符串

  select first_name||'.'||last_name

  from s_emp;

      等价于

  select concat(first_name,concat('.',last_name))

  from s_emp;

(5)SUBSTR:字符串截取函数

  substr(参数1,参数2,参数3)

  参数1:截取的字符串

  参数2:开始截取的位置,从1开始数

  参数3:截取的长度,如果没有参数3,则截取到最后

  例:select substr('hello',-2) from dual;      lo

    select substr('hello',0,2) from dual;     he

    select substr('hello',2,3) from dual;     ell

(6)LENGTH:字符串长度函数

  select first_name,length(first_name)

  from s_emp;

(7)REPLACE:字符串替换函数

  select replace(first_name,'Bela','Haha')

  from s_emp;

2,日期函数

(1)sysdate:系统当前时间

  select sysdate from dual;

(2)months_between:两个日期相差多少个月

  select months_between(sysdate+31,sysdate) from dual;          1  
  select months_between('07-1月-16','07-1月'-15) from dual;     12

(3)add_months:返回给定日期+n个月后的那个日期

  select months_between('07-1月-15',12) from dual;    07-1月-16

  select months_between('07-1月-15',11.5) from dual;    07-12月-15

(4)NEXT_DAY:返回即将来临的星期几的日期

  select next_day(sysdate,'星期一') from dual;

(5)LAST_DAY:返回指定日期所在月份的最后一天的那个日期

  select last_day(sysdate) from dual;

(6)round:四舍五入    month:15舍16入   year:6舍7入

   trunc:只舍不入

  select round(to_date('07-1月-16'),'month') from dual;      01-1月-16

  select trunc(to_date('07-1月-16'),'year') from dual;        01-1月-16

3,数字函数

(1)round和trunc 函数

  例:按每月30天计算员工每日薪水,要求计算结果四舍五入到小数点后两位

  select round(salary/30,2) from s_emp;

   select trunc(146.925,-2) from dual;    100

      查询员工工作了多少个月

  select first_name,trunc(months_between(sysdate,start_date)) from s_emp;

(2)floor向下取

       select floor(1.9) from dual;   1

 (3)  ceil向上取

   select ceil(1.4) from dual;    2

(4)abs 绝对值

  select abs(-2.5) from dual;      2.5

(5)mod取余

  select mod(10,3) from dual;     1

(6)power(数字,n次方)

  select power(2,3) from dual;     8

4,转换函数

(1)to_char:将数字或者日期类型的数据转换为字符串类型

    select to_char(123,'$99,999.00') from dual;
    select to_char(sysdate,'yyyy-mm-dd') from dual;
    select to_char(sysdate,'yyyy mm d dd ddd year month ddsp ddspth day dy') from dual;    
    select to_char(sysdate,'yyyy-mm-dd HH24:MI:SS') from dual;

(2)to_number:将字符串数据转换为数字类型

  select to_number('123')+to_number('456') from dual;   579

  select to_number('avc') from dual;     报错

(3)to_date:将字符串转化为日期类型

  alter session set nls_date_format='yyyy-mm-dd';
     select to_date('2015-09-16','yyyy-mm-dd') from dual;

原文地址:https://www.cnblogs.com/sq-software/p/5105939.html