ORACLE⽇期时间格式化参数详解ORACLE⽇期时间格式化参数详解
格式化⽇期指的是将⽇期转为字符串,或将字符串转为⽇期,下⾯⼏个函数可以⽤来格式化⽇期
TO_CHAR(datetime, 'format')
TO_DATE(character, 'format')
TO_TIMESTAMP(character, 'format')
TO_TIMESTAMP_TZ(character, 'format')
SQL 产⽣的结果
SELECT TO_CHAR(current_timestamp, 'format') FROM DUAL;
1
Format List item
YYYY-MM-DD2015-06-15
YYYY-MON-DD2015-JUN-15
YYYY-MM-DD HH24:MI:SS FF32015-06-15 13:18:10 700
YYYY-MM-DD HH24:MI:SS FF3 TZR2015-06-15 13:18:10 700 +08:00
DS6/15/2015
DL Monday, June 15, 2015
TS1:18:10 PM
Oracle ⽀持哪些格式呢?看看下⾯的表格吧
Format Description
Y年的最后⼀位数字,如:5
YY年的最后两位数字,如:15
YYY年的最后三位数字,如:015
YYYY年,如:2015
Y,YYY年⽤逗号分割
SYYYY年
YEAR年拼写,如:TWENTY FIFTEEN
SYEAR年拼写,如:TWENTY FIFTEEN
I ISO年的最后⼀位数字,如:5
IY ISO年的最后两位数字,如:15
IYY ISO年的最后三位数字,如:015
IYYY ISO年,如:2015
RR
两位数字年,如:15
RRRR
四位数字年,如:2015
MM Month (01-12)
MON⽉份简称,如:JUN
MONTH⽉份全称,如:JUNE
RM罗马数字⽉份
D Day of week (1-7)
DD Day of month (1-31)
DDD Day of year (1-366)
HH Hour of day (1-12)
HH12Hour of day (1-12)
HH24Hour of day (0-23)
MI Minute (0-59)
SS Second (0-59)
SSSSS Seconds past midnight
FF [1…9]毫秒
DS⽇期简称,如:6/12/2015
DL⽇期全称,如:Friday, June 12, 2015
TS时间简称,如:5:18:03 PM
CC世纪,如:21
SCC世纪,如:21
Q Quarter of year (1, 2, 3, 4)
W Week of month (1-5)
WW Week of year (1-53)
IW ISO Week of year (1-52 or 1-53)
DY星期简称,如:Fri
DAY星期全称,如:Friday
AM    A.M.
PM P.M.
AD    A.D.
BC    B.C.
TZD夏令时
TZR时区
TZH时区之时差
TZM时区之分钟差
EE era 全称
E era 简称
J The number of days since January 1, 4712 BC
FM去掉⾸尾空格
FX精确匹配
X秒和毫秒分隔符
TH DDTH --> 4th
SP DDSP -->FOUR
SPTH DDSPTH --> FOURTH
THSP DDTHSP --> FOURTH
默认情况下,Oracle 格式化⽇期时,有⼀定的容错性,如下⾯的 SQL 返回正确的结果。
TO_DATE格式(以时间:2007-11-02  13:45:25为例)
Year:
yy two digits 两位年显⽰值:07
yyy three digits 三位年显⽰值:007
yyyy four digits 四位年显⽰值:2007
Month:
mm    number    两位⽉显⽰值:11
mon    abbreviated 字符集表⽰显⽰值:11⽉,若是英⽂版,显⽰nov
month spelled out 字符集表⽰显⽰值:11⽉,若是英⽂版,显⽰november
Day:
dd    number        当⽉第⼏天显⽰值:02
ddd    number        当年第⼏天显⽰值:02
dy    abbreviated 当周第⼏天简写显⽰值:星期五,若是英⽂版,显⽰fri
day    spelled out  当周第⼏天全写显⽰值:星期五,若是英⽂版,显⽰friday
ddspth spelled out, ordinal twelfth
Hour:
hh    two digits 12⼩时进制显⽰值:01
hh24 two digits 24⼩时进制显⽰值:13
Minute:
mi    two digits 60进制显⽰值:45
Second:
ss    two digits 60进制显⽰值:25
其它
Q    digit        季度显⽰值:4
WW    digit        当年第⼏周显⽰值:44
W    digit          当⽉第⼏周显⽰值:1
24⼩时格式下时间范围为: 0:00:00 - 23:
12⼩时格式下时间范围为: 1:00:00 - 12:59:59 ....
1. ⽇期和字符转换函数⽤法(to_date,to_char)
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;  //⽇期转化为字符串
select to_char(sysdate,'yyyy') as nowYear  from dual;  //获取时间的年
select to_char(sysdate,'mm')    as nowMonth from dual;  //获取时间的⽉
select to_char(sysdate,'dd')    as nowDay    from dual;  //获取时间的⽇
select to_char(sysdate,'hh24') as nowHour  from dual;  //获取时间的时
select to_char(sysdate,'mi')    as nowMinute from dual;  //获取时间的分
select to_char(sysdate,'ss')    as nowSecond from dual;  //获取时间的秒
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')    from dual//
2.
select to_char( to_date(222,'J'),'Jsp') from dual
显⽰Two Hundred Twenty-Two
3.求某天是星期⼏
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;
星期⼀
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;        monday
设置⽇期语⾔
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
也可以这样
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. 两个⽇期间的天数
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
5. 时间为null的⽤法
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual;
注意要⽤TO_DATE(null)
6.⽉份差
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
那么12⽉31号中午12点之后和12⽉1号的12点之前是不包含在这个范围之内的。
所以,当时间需要精确的时候,觉得to_char还是必要的
7. ⽇期格式冲突问题
输⼊的格式要看你安装的ORACLE字符集的类型, ⽐如: US7ASCII, date格式的类型就是: '01-Jan-01'
alter system set NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
或者在to_date中写
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
注意我这只是举了NLS_DATE_LANGUAGE,当然还有很多,
可查看
select * from nls_session_parameters
select * from V$NLS_PARAMETERS
8.
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-
02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )
查2002-02-28⾄2002-02-01间除星期⼀和七的天数
在前后分别调⽤DBMS_UTILITY.GET_TIME, 让后将结果相减(得到的是1/100秒, ⽽不是毫秒).
9. 查⽉份
select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1
select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1.03225806451613
10. Next_day的⽤法
Next_day(date, day)
格式化命令format参数Monday-Sunday, for format code DAY
Mon-Sun, for format code DY
1-7, for format code D
11
select to_char(sysdate,'hh:mi:ss') TIME from all_objects
注意:第⼀条记录的TIME 与最后⼀⾏是⼀样的
可以建⽴⼀个函数来处理这个问题
create or replace function sys_date return date is
begin
return sysdate;
end;
select to_char(sys_date,'hh:mi:ss') from all_objects;
12.获得⼩时数
extract()出⽇期或间隔值的字段值
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer
SQL> select sysdate ,to_char(sysdate,'hh') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH')
-------------------- ---------------------
2003-10-13 19:35:21 07
SQL> select sysdate ,to_char(sysdate,'hh24') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH24')
-------------------- -----------------------
2003-10-13 19:35:21 19
13.年⽉⽇的处理
select older_date,
newer_date,
years,
months,
abs(
trunc(
newer_date-
add_months( older_date,years*12+months )
)
) days
from ( select
trunc(months_between( newer_date, older_date )/12) YEARS,
mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,
newer_date,
older_date
from (
select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date
from emp
)
)
14.处理⽉份天数不定的办法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
16.出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
闰年的处理⽅法
to_char( last_day( to_date('02'    | | :year,'mmyyyy') ), 'dd' )
如果是28就不是闰年
<与rrrr的区别
'YYYY99 TO_C
------- ----
yyyy 99 0099
rrrr 99 1999
yyyy 01 0001
rrrr 01 2001
18.不同时区的处理
select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate
from dual;
19.5秒钟⼀个间隔
Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')        from dual
2002-11-1 9:55:00 35786
SSSSS表⽰5位秒数
20.⼀年的第⼏天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
310 2002-11-6 10:03:51
21.计算⼩时,分,秒,毫秒
select
Days,
A,
TRUNC(A*24) Hours,
TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,
TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,
TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds
from
(
select
trunc(sysdate) Days,
sysdate - trunc(sysdate) A
from dual
)
select * from tabname
order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');

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