设为首页 加入收藏

TOP

oracle常用函数(2)(一)
2015-07-24 11:01:29 来源: 作者: 【 】 浏览:1
Tags:oracle 常用 函数

1、TRIM([ { { LEADING | TRAILING | BOTH }[ trim_character ]| trim_character} FROM ]trim_source) 函数
参数:leading 如果指定leading参数,oracle数据库将去除任何等于trim_character的开头字符。
参数:trailing 如果指定traling参数,oracle将去除任何等于trim_character的结尾字符。
参数:both 如果指定了both参数或者三个参数都未指定,oracle将去除任何等于trim_character的开头和结尾字符。
参数:trim_character 如果没有指定trim_character参数,默认去除的值为空格。
参数:trim_source 如果只指定修剪源(trim_source),oracle将去除trim_source的开头和结尾的空格。
注意:a、trim函数返回一个varchar2类型值。该值最大的长度等于trim_source的长度。
b、如果trim_source和trim_character有一个为null,则trim函数返回null。
c、trim_character和trim_source都可以为以下任意一种数据类型:CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, OR NCLOB。
d、返回值的类型与trim_source的数据类型一致。

select trim(leading 'B' from 'BBoat is good') "test_trim" from dual;--oat is good
select trim(trailing 'd' from 'BBoat is good') "test_trim" from dual;--BBoat is goo
select trim(both 'd' from 'd--Boat is handsome--d') from dual t;----Boat is handsome--
select trim('d' from 'd--Boat is handsome--d') from dual t;----Boat is handsome--
select length(trim(leading from ' d--Boat is handsome--d ')) from dual t;--23
select length(trim(' d--Boat is handsome--d ')) from dual t;--22
select trim(trailing null  from 'boat ') nulltrim from dual;
select trim(both 'b'  from null) nulltrim from dual;

2、abs函数

用途:返回指定值的绝对值

select abs(100),abs(-100) from dual;--100 100
select abs(null) from dual t;
select abs('') from dual t;

3、ceil函数

用途:返回大于或等于给出数字的最小整数

select ceil(-7.09) from dual t;-- -7
select ceil(7.09) from dual t;-- 8

4、floor函数

用途:返回小于或等于给定数字的最大整数

select floor(-7.09) from dual;-- -8
select floor(7.09) from dual;-- 7

5、mod(a,b)函数

用途:返回一个a除以b的余数

select mod(2,3) from dual;--2
select mod(-2,3) from dual;-- -2

6、power(a,b)函数

用途:返回a的b次方函数

select power(2,3) from dual;

7、round(number)函数

用途:四舍五入的方式取给定数字的整数值

select round(1.4) from dual;--1

round(带小时分秒的日期,参数)

参数:秒SS,精确到秒。
参数:分MI,到分,秒数30是分界线,相当于四舍五入的5,如果秒超过30,向前进1分钟
参数:时HH,如果分超过30分钟,向前进1个小时
参数:日DD, 如果小时超过12点,向前进1个小时
参数:月MM,16号是临界,相当于四舍五入的5,如果超过16号,向前进1天
参数:年YY,超过7月,向前进1年
参数:day,星期三是分界线 大于星期三即显示日期所在星期的下一个星期日,小于等于则显示所在星期的星期日

select round(to_date('2014-06-30','yyyy-mm-dd'),'yyyy') nian from dual;--2014/1/1
select round(to_date('2014-07-01','yyyy-mm-dd'), 'yyyy') nian from dual;--2015/1/1

select round(to_date('2014-06-16','yyyy-mm-dd'), 'mm') yue from dual;--2014/7/1
select round(to_date('2014-06-15','yyyy-mm-dd'), 'mm') yue from dual;--2014/6/1

select round(to_date('2014-07-15 12:00:01','yyyy-mm-dd hh24:mi:ss'), 'dd') ri from dual;--2014/7/16
select round(to_date('2014-07-15 11:59:59','yyyy-mm-dd hh24:mi:ss'), 'dd') ri from dual;--2014/7/15

select round(to_date('2014-07-30 10:30:01','yyyy-mm-dd hh24:mi:ss'), 'hh') shi from dual;--2014/7/30 11:00:00
select round(to_date('2014-07-30 10:29:59','yyyy-mm-dd hh24:mi:ss'), 'hh') shi from dual;--2014/7/30 10:00:00

select round(to_date('2014-07-15 13:30:01','yyyy-mm-dd hh24:mi:ss'), 'mi') fen from dual;--2014/7/15 13:30:00
select round(to_date('2014-07-15 14:30:31','yyyy-mm-dd hh24:mi:ss'), 'mi') fen from dual;--2014/7/15 1:31:00

select sysdate,round(sysdate-1, 'day'),round(sysdate, 'day'),round(sysdate+1,'day') from dual;

8、ADD_MONTHS函数

用途:增加或减去指定月份

select add_months(to_date('2014-7-30','yyyy-mm-dd'),2) from dual;--2014/9/30
select add_mon
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Oracle报错:"ORA-18008: 无.. 下一篇Oracle安装报错[INS-06101]IPaddr..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)