oracle学习笔记,九 SQL常用函数说明以及使用

SQL常用函数说明以及使用

以下补充以下常用的函数,更多的请看oracle函数API文档

to_char

to_char(8.58,'9.99')
to_char(8.50,'9.00')
to_char(8.50,'9.99')
create table employee(
        id number,
        crete_date date timestamp default localtimestamp --毫秒级时间戳,插入一条数据会自动填写 
);

to_date

--将2019-5-11字符串转为date类型
select to_date('2019-5-11','yyyy-mm-dd') date1;
--2019年5月22日字符串转为date类型
to_date('2019年5月22日','yyyy"年"mm"月"dd"日"') date2 from dual;

to_number

select to_nuber('1,234','9,999') --第二个参数,是格式

nvl 空值赋值

nvl(sal,0)   
--不为空,返回sal,如果为空,就返回0   
nvl2(sal,sal,0)         
--不为空,返回工资本身,为空,返回0             

字符串处理

  • ltrim

    删除左边空白符或指定字符

ltrim('   here',' ') --删除空白符(空格)
ltrim('---hello world','-') --删除“-”,最终结果显示为hello world
ltrim('  hello world') --删除空格
  • rtrim

    删除右边空白符或指定字符,与上面类似

  • trim

    删除空白符或制定字符,与上面类似

  • substr

    截取字符

decode 条件取值

decode(age,10,'少年',20,'青年',中年)
--相当于switch,age=10,返回少年,age=20,返回青年,其他的则返回中年

数学函数

  • abs

    绝对值

  • ceil

    返回较大的最小整数

ceil(7.6)
--返回8
  • floor

    返回较小的最大整数

round(7.6)
--返回7
  • round

    返回四舍五入的数值

Select round(100.256,2) from dual; --返回100.26
select round(100.256,3) from dual;  --返回100.256
  • trunc

    截取

Select trunc(100.256,2) from dual; --返回100.25
select trunc(100.256,3) from dual;  --返回100.256
  • power

    幂次方

  • mod

    取余数

  • sqrt

    平方根