Oracle中TO_DATE格式2016-12-23 13:20:01
TO_DATE格式(以时间:2016-12-23 13:20:01为例)
Year:
yy two digits 两位年 显示值:16
yyy three digits 三位年 显示值:016
yyyy four digits 四位年 显示值:2016
Month:
mm number 两位月 显示值:12
mon abbreviated 字符集表示 显示值:12月,若是英文版,显示dec
month spelled out 字符集表示 显示值:12月,若是英文版,显示december
Day:
dd number 当月第几天 显示值:23
ddd number 当年第几天 显示值:23
dy abbreviated 当周第几天简写 显示值:星期五,若是英文版,显示fri
day spelled out 当周第几天全写 显示值:星期五,若是英文版,显示friday
Hour:
hh two digits 12小时进制 显示值:01
hh24 two digits 24小时进制 显示值:13
Minute:
mi two digits 60进制 显示值:20
Second:
ss two digits 60进制 显示值:01
其它:
Q digit 季度 显示值:4
WW digit 当年第几周 显示值:44
W digit 当月第几周 显示值:1
> 24小时格式下时间范围为: 0:00:00 - 23:59:59....
> 12小时格式下时间范围为: 1:00:00 -12:59:59 ....
1.日期和字符转换函数用法(to_date,to_char)
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss day') 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('2016-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual;
2016/12/23 星期五 11:39:44
select to_char( to_date(2016,'J'),'Jsp') from dual ;
Two Thousand Sixteen
2.求某天是星期几
select to_char(to_date('2016-12-23','yyyy-mm-dd'),'day') from dual;
//星期五
select to_char(to_date('2016-12-23','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; //friday
//设置日期语言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
//也可以这样
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
3. 两个日期间的天数
select floor(to_date('20161222','yyyymmdd') - to_date('20161022','yyyymmdd')) from dual;
//61
4.时间为null的用法
select 1, TO_DATE(null) from dual; //注意要用TO_DATE(null)
5. 日期格式冲突问题
//输入的格式要看你安装的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;
6. 查找月份
select months_between(to_date('20161222','yyyymmdd') , to_date('20161022','yyyymmdd')) "MONTHS" FROM DUAL;
// 1
7.找出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual ;
//366
8.一年的第几天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
//359 2016/12/24 星期六 11:57:51
9.计算小时,分,秒,毫秒
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
)
10.trunc[截断到最接近的日期,单位为天] ,返回的是日期类型
select sysdate S1,
trunc(sysdate) S2, //返回当前日期,无时分秒
trunc(sysdate,'year') YEAR, //返回当前年的1月1日,无时分秒
trunc(sysdate,'month') MONTH , //返回当前月的1日,无时分秒
trunc(sysdate,'day') DAY //返回当前星期的星期天,无时分秒
from dual
11.计算时间差 注:oracle时间差是以天数为单位,所以换算成年月,日
//时间差-年
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual
//时间差-月
select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual
//时间差-天
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual
//时间差-时
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual
//时间差-分
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual
//时间差-秒
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual