oracle中to_date详细⽤法⽰例(oracle⽇期格式转换)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; //获取时间的秒
2. 字符串和时间互转
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual
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秒, ⽽不是毫秒)
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)
Monday-Sunday, for format code DAY
Mon-Sun, for format code DY
1-7, for format code D
11.获得⼩时数
//extract()出⽇期或间隔值的字段值
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer select sysdate ,to_char(sysdate,'hh') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH')
-------------------- ---------------------
2003-10-13 19:35:21 07
select sysdate ,to_char(sysdate,'hh24') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH24')
-------------------- -----------------------
2003-10-13 19:35:21 19
12.年⽉⽇的处理
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
)
)
13.处理⽉份天数不定的办法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual 14.出今年的天数
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
16.不同时区的处理
select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,
sysdate from dual;
17. 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位秒数
18.⼀年的第⼏天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
//310 2002-11-6 10:03:51
19.计算⼩时,分,秒,毫秒
SELECT
Days,
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')
// floor((date2-date1) /365) 作为年
// floor((date2-date1, 365) /30) 作为⽉
/
/ d(mod(date2-date1, 365), 30)作为⽇.
<_day函数
//返回下个星期的⽇期,day为1-7或星期⽇-星期六,1表⽰星期⽇
next_day(sysdate,6)是从当前开始下⼀个星期五。后⾯的数字是从星期⽇开始算起。
// 1 2 3 4 5 6 7
//⽇⼀⼆三四五六
select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from dual //⽇期返回的是天然后转换为ss
21,round[舍⼊到最接近的⽇期](day:舍⼊到最接近的星期⽇)
select sysdate S1,
round(sysdate) S2 ,
round(sysdate,'year') YEAR,
round(sysdate,'month') MONTH ,
round(sysdate,'day') DAY from dual
22,trunc[截断到最接近的⽇期,单位为天] ,返回的是⽇期类型
select sysdate S1,
trunc(sysdate) S2, //返回当前⽇期,⽆时分秒
trunc(sysdate,'year') YEAR, //返回当前年的1⽉1⽇,⽆时分秒
trunc(sysdate,'month') MONTH , //返回当前⽉的1⽇,⽆时分秒
trunc(sysdate,'day') DAY //返回当前星期的星期天,⽆时分秒
from dual
23,返回⽇期列表中最晚⽇期
select greatest('01-1⽉-04','04-1⽉-04','10-2⽉-04') from dual
24.计算时间差
:
注: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',oracle中trunc函数用法
'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 //时间差-秒
25.更新时间
//oracle时间加减是以天数为单位,设改变量为n,所以换算成年⽉,⽇
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n*365,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-年select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
add_months(sysdate,n) as newTime from dual //改变时间-⽉
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-⽇select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n/24,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-时select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n/24/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-分select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n/24/60/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-秒26.查⽉的第⼀天,最后⼀天
SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,
LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month
FROM dual;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论