当前位置: 代码迷 >> 综合 >> ORACLE中关于时间日期的一些东西
  详细解决方案

ORACLE中关于时间日期的一些东西

热度:79   发布时间:2023-10-18 00:23:38.0

常常为各式各样的日期和时间问题困扰,总结了一些常用的日期和时间的语句,希望对大家有所帮助:

一:日期转字符

1. 加上24就是以24小时计时方式。

--select to_char(sysdate, 'yyyymmdd hh24mmss') from dual     执行结果:20190108 180105

-- select to_char(sysdate, 'yyyymmdd hhmmss') from dual        执行结果:20190108 060105 

这里和java中的还不一样:大写HH和小写hh

new SimpleDateFormat("yyyyMMdd hhmmss").format(new Date());//输出:20190108 061501
new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date());//输出:20190108 181501

二:字符转日期

1. --select to_date('20180103 18:42:00', 'yyyymmdd HH24:MI:SS') from dual     //精确到秒级的

2.--select date'2017-12-01'  from dual    //简洁日期式的

三:查询指定的日期

---trunc()函数的用法 

1.select to_char(trunc(add_months(last_day(sysdate), -1) + 1), 'yyyy-mm-dd')  from dual   本月第一天,上月最后一天在加一天

2.select to_char(last_day(sysdate), 'yyyy-mm-dd') from dual   本月最后一天
3.select trunc(sysdate) from dual  --2011-3-18  今天的日期为2011-3-18 
4.select trunc(sysdate, 'mm')   from   dual  --2011-3-1    返回当月第一天. 
5.select trunc(sysdate,'yy') from dual  --2011-1-1       返回当年第一天 
6.select trunc(sysdate,'dd') from dual  --2011-3-18    返回当前年月日 
7.select trunc(sysdate,'yyyy') from dual  --2011-1-1   返回当年第一天 
8.select trunc(sysdate, 'hh') from dual   --2011-3-18 14:00:00   当前时间为14:41   
9.select trunc(sysdate, 'mi') from dual  --2011-3-18 14:41:00   TRUNC()函数没有秒的精确

---TRUNC(number,num_digits) 
Number 需要截尾取整的数字。 
Num_digits 用于指定取整精度的数字。Num_digits 的默认值为0。 
TRUNC()函数截取时不进行四舍五入 
1.select trunc(123.458) from dual --123 
2.select trunc(123.458,0) from dual --123 
3.select trunc(123.458,1) from dual --123.4 
4.select trunc(123.458,-1) from dual --120 
5.select trunc(123.458,-4) from dual --0 
6.select trunc(123.458,4) from dual  --123.458 
7.select trunc(123) from dual  --123 
8.select trunc(123,1) from dual --123 

9.select trunc(123,-1) from dual --120

四:其他用法

1.查询某天星期几

select to_char(to_date('2004-12-27','yyyy-mm-dd'),'day') from dual;     执行结果:monday

2.两个日期间隔的天数

select floor(sysdate -to_date('2002/04/05','yyyy/mm/dd')) from dual;   执行结果:6123

3.TO_DATE格式  
Day:  
dd number 12  
dy abbreviated fri  
day spelled out friday  
ddspth spelled out, ordinal twelfth 

4.Month:  
mm number 03  
mon abbreviated mar  
month spelled out march  
5.Year:  
yy two digits 98  
yyyy four digits 1998

6. 时间为null的用法  
select id, active_date from table1  
UNION  
select 1, TO_DATE(null) from dual;  
注意要用TO_DATE(null)  
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')) from dual;  
1  

select months_between(to_date('02-01-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) 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  
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.  
获得小时数  

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就不是闰年  

17.  
yyyy与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

 

  相关解决方案