Oracle⽇期函数和转换函数
⼀、⽇期函数
⽇期函数⽤于处理date类型的数据,两个⽇期相减返回⽇期之间相差的天数。⽇期不允许做加法运算,⽆意义。
常见代表符号:yyyy 年,mm ⽉,dd ⽇,hh ⼩时,mi 分钟,ss 秒,day 星期
默认情况下⽇期格式是dd-mon-yy即12-3⽉-19
(1)sysdate: 该函数返回系统时间
(2)months_between(m,n)⽇期m和⽇期n相差多少⽉数
(3)add_months(d,n)在⽇期d上增加n个⽉数
(4)next_day(d, '星期*') 指定⽇期d下⼀个星期*对应的⽇期
(5)last_day(d):返回指定⽇期d所在⽉份的最后⼀天
(6)extract(month from d)从⽇期d上提取⽉份数
(7)round(d,time)⽇期的四舍五⼊
(8)trunc(d,time)⽇期的截断
以下是⽇期函数的⼀些例⼦及效果图:
各种情况例⼦:
months_between select months_between('01-9⽉-95','11-1⽉-94') from dual;  --19.6774193548387
add_months select add_months('11-2⽉-18',6) from dual;  --2018/8/11
next_day select next_day('11-2⽉-18','星期六') from dual; --2018/2/17
last_day select last_day('11-2⽉-18') from dual; --2018/2/28
round 四舍五⼊⽉份 25-7⽉-18 select round(to_date('25-7⽉-2018'), 'month') from dual;  --2018/8/1
round 四舍五⼊年份 25-7⽉-18 select round(to_date('25-7⽉-2018’), 'year') from dual; --2019/1/1
trunc 截断⽉份 25-7⽉-18 select trunc(to_date('25-7⽉-2018'), 'month') from dual; --2018/7/1
trunc 截断年份 25-7⽉-18 select trunc(to_date('25-7⽉-2018'), 'year') from dual; --2018/1/1
trunc 当前⽇期的00点00分00秒 select trunc(sysdate) from dual;
sql⽐较⽇期
今天之前:
select * from table where update < to_date('2012-09-07 00:00:00', 'yyyy-MM-dd HH24:mi:ss');
精确时间:
select * from table where update = to_date('2012-09-07 00:00:00', 'yyyy-MM-dd HH24:mi:ss');
某段时间内:
select * from table where update <= to_date('2012-09-07 00:00:00', 'yyyy-MM-dd HH24:mi:ss')
and update >= to_date('2010-02-07 00:00:00', 'yyyy-MM-dd HH24:mi:ss')
eg:查已经⼊职8个⽉多的员⼯
SQL>
select * from emp
where sysdate>=add_months(hiredate,8);
eg:显⽰满10年服务年限的员⼯的姓名和受雇⽇期。
SQL>
select ename, hiredate from emp
where sysdate>=add_months(hiredate,12*10);
eg:对于每个员⼯,显⽰其加⼊公司的天数。
SQL> select floor(sysdate-hiredate),ename from emp;
或者
oracle四舍五入
SQL> select trunc(sysdate-hiredate),ename from emp;
eg:出各⽉倒数第3天受雇的所有员⼯。
SQL>select hiredate,ename from emp where last_day(hiredate)-2=hiredate;
⼆、转换函数
转换函数⽤于将数据类型从⼀种转为另外⼀种。在某些情况下,oracle server允许值的数据类型和实际的不⼀样,这时oracle server会隐含的转化数据类型
我们要说的是尽管oracle可以进⾏隐含的数据类型的转换,但是它并不适应所有的情况,为了提⾼程序的可靠性,我们应该使⽤转换函数进⾏转换。
(1)to_char函数
格式: to_char(date,‘format’)
1、必须包含在单引号中⽽且⼤⼩写敏感。
2、可以包含任意的有效的⽇期格式。
3、⽇期之间⽤逗号隔开。
eg:⽇期是否可以显⽰时/分/秒
SQL> select ename, to_char(hiredate,'yyyy-mm-dd hh24:mi:ss') from emp;
eg:薪⽔是否可以显⽰指定的货币符号
SQL> select sal,to_char(sal,'$999,999.99') from emp;
yy:两位数字的年份2004–>04
yyyy:四位数字的年份 2004年
mm:两位数字的⽉份 8⽉–>08
dd:两位数字的天 30号–>30
hh24: 8点–>20
hh12:8点–>08
mi、ss–>显⽰分钟/秒
9:显⽰数字,并忽略前⾯0
0:显⽰数字,如位数不⾜,则⽤0补齐
.:(⼩数点)在指定位置显⽰⼩数点
,:(千位符)在指定位置显⽰逗号
$:(美元符)在数字前加美元
L:(本地货币符)在数字前⾯加本地货币符号
C:(国际货币符)在数字前⾯加国际货币符号
eg:显⽰薪⽔的时候,把本地货币单位加在前⾯
SQL> select ename, to_char(sal,'L99999.99')from emp;
eg:显⽰1980年⼊职的所有员⼯
SQL> select * from emp where to_char(hiredate, 'yyyy')=1980;
eg:显⽰所有12⽉份⼊职的员⼯
SQL> select * from emp where to_char(hiredate, 'mm')=12;
这⾥的12和1980是数字,可以加 ’ ’ 也可以不加,因为Oracle会⾃动转换,但是最好加。eg:显⽰姓名、hiredate和雇员开始⼯作⽇是星期⼏
SQL> select ename,hiredate,to_char(hiredate,'day') from emp;
(2)to_date函数
格式:to_date(string,‘format’)
函数to_date⽤于将字符串转换成date类型的数据。
eg:把字符串2015-03-18 13:13:13转换成⽇期格式,
SQL> select to_date('2015-03-18 13:13:13','yyyy-mm-dd hh24:mi:ss') from dual;
(3)to_number函数
格式:to_number(char,‘format’)
使⽤to_number函数将字符转换成⽇期。
SQL> select to_number('¥1,234,567,890.00','L999,999,999,999.99') from dual;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。