您的位置:首页 > 数据库 > Oracle

Oracle--单行函数

2016-04-25 20:36 483 查看
关于单行函数是Oracle的基础内容,博主只是想要复习一遍,如果想要更细致的内容,请移步

单行函数分为5类:字符函数、数值函数、日期函数、转换函数、通用函数

以下使用到的emp表为oracle自带的

1.字符函数

lower(chars):将字符串转为小写

<span style="font-size:14px;">select lower(ename) from emp;
</span>


upper(chars):转为大写

<span style="font-size:14px;">select <span style="font-size:14px;">upper</span>(ename) from emp;</span>


initcap(chars):首字母大写

<span style="font-size:14px;">select <span style="font-size:14px;">initcap</span>(ename) from emp;</span>


length(chars):求字符串的字符长度

<span style="font-size:14px;">select length(ename) from emp;
而lengthb统计的是字节数
</span>


substr(chars,start,end):截取字符串

<span style="font-size:14px;">select substr(ename,1,3) from emp;
SMITH的结果:SMI, 说明substr的截取范围是[1,3],同时也说明角标从1开始计数
</span>


instr(charA,charB):检索charB在charA中第一次出现的位置

<span style="font-size:14px;">select instr('aqameda','me') from dual;
结果:INSTR('AQAMEDA','ME')
---------------------
                    4
说明角标也是从1开始的
</span>


lpad和rpad:在左边或右边填充n个字符

<span style="font-size:14px;">select lpad('foo',5,'*') from dual;
结果:**foo
可能大家已经发现了,只填充了2个*号,这是因为foo占了3,这里的填充指的是把foo填充到5位</span>


trim(char1 from char2):去前后的指定字符

<span style="font-size:14px;"> select trim('q' from 'qqaqq') from dual;
注意,只能指定一个要去掉的字符,不能是字符串,去空格指定为' '即可
</span>


replace(chars,old,new):替换

<span style="font-size:14px;">select replace('memeda','e','d') from dual;
select replace('memeda','me','dq') from dual;
可以替换字符串</span>


concat(char1,char2):字符串连接

select concat('my name is :',ename) from emp;
结果:my name is :SMITH
my name is :ALLEN


2.数值函数

round(num,i):四舍五入

select round(84.517,1) from dual;
结果:84.5
84.517保留一位小数,可以传入负数,例如:<pre name="code" class="html">round(84.517,-1)的结果为80



trunc(num,n):截取n位小数

<span style="font-size:14px;">select trunc(84.517,0) from dual;
结果:84
</span>


mod(num1,num2):求num1除以num2的余数

<span style="font-size:14px;">select mod(100,30) from dual;
结果为10</span>


3.日期函数

补充知识:sysdate表示当前时间



months_between(date1,date2): 两个时间之间的月数

select months_between(hiredate,sysdate) from emp;
计算员工入职月数


给时间加上月数

<span style="font-size:14px;">select add_months(sysdate,12) from dual;
</span>


last_day(date): 本月最后一天

<span style="font-size:14px;">select last_day(sysdate) from dual;</span>


next_day(date,星期n):下一个星期几

<span style="font-size:14px;">select next_day(sysdate,'星期六') from dual;
下一个星期六,但是不能写礼拜六
</span>


ound(trunc)也可以对日期四舍五入,规则是一个月有没有过一半,一年有没有过一半

4.转换函数

oracle的转换分为显式转换和隐式转换,隐式转换由oracle自动完成,我们并不关心

oracle的显示转换:数字--字符串,字符串---日期之间的转换

to_char():

1.由时间转为字符串

to_char(sysdate,'yyyy-mm-dd "当前时间:"hh24:mi:ss')

"当前时间"使用了双引号,hh24表示用24小时制

2.由数字转为字符串

to_char(sal,'L9,999.99'):L表示本地货币符号,9表示一位数,逗号表示千位符

to_number():to_number('$5,000.00','$9,999.99')

to_date():to_date('2016-05-15','yyyy-mm-dd')

5.通用函数

nvl(a,b):等同于mysql中的ifnull,表示a为null是,用b代替

<span style="font-size:14px;">select nvl(comm,0) from emp;</span>


nvl2(a,b,c):如果a为null,返回c,否则返回b

<span style="font-size:14px;">select nvl2(comm,10,0) from emp;
nvl2是对nvl的增强,nvl在a不为null时只能返回其本身</span>


nullif(a,b):如果a=b,返回null,否则返回a

coalesce(列1,列2): 从左到右 找到第一个不为null的值

select comm,sal,coalesce(sal,comm) from emp

COMM        SAL COALESCE(SAL,COMM)
----- ---------- ------------------
800                800
300       1600               1600
500       1250               1250
2975               2975
1400       1250               1250
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: