当前时间最接近的刻钟
SELECT trunc(SYSDATE,'hh24'),CASE WHEN to_char(SYSDATE,'mi') <8 THEN 0
WHEN to_char(SYSDATE,'mi') <15 AND to_char(SYSDATE,'mi')>8 THEN 15
WHEN to_char(SYSDATE,'mi') >15 AND to_char(SYSDATE,'mi')<23 THEN 15
WHEN to_char(SYSDATE,'mi') >23 AND to_char(SYSDATE,'mi')<30 THEN 30
WHEN to_char(SYSDATE,'mi') >30 AND to_char(SYSDATE,'mi')<38 THEN 30
WHEN to_char(SYSDATE,'mi') >38 AND to_char(SYSDATE,'mi')<45 THEN 45
WHEN to_char(SYSDATE,'mi') >45 AND to_char(SYSDATE,'mi')<53 THEN 45
WHEN to_char(SYSDATE,'mi') >45 AND to_char(SYSDATE,'mi')<53 THEN 0
END timer
FROM dual
当前日期星期几
select to_char(SYSDATE,'day') from dual;
SELECT to_char(SYSDATE,'DD') FROM dual
SELECT to_char(SYSDATE,'DDD') FROM dual //一年的第几天
SELECT to_char(SYSDATE,'D') FROM dual //西方一般周日当作一周的开始
或者
指定日期是星期几
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','nls_date_language=american') from dual;
当前时间是第几周
select to_char(SYSDATE,'ww') from dual;
抽取当前时间分钟数
SELECT to_char(SYSDATE,'mi') FROM dual
抽取当前时间的24小时制的小时数
SELECT to_char(SYSDATE,'HH24') FROM dual
抽取当前时间的月份
SELECT EXTRACT(MONTH FROM SYSDATE) FROM DUAL
两个日期之间的天数
select floor(sysdate - to_date('20101103','yyyymmdd')) from dual;
两个日期的秒数
select (sysdate - to_date('20101103','yyyymmdd'))*24*60*60 from dual;
处理月份和天数不定的办法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
算出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
取时区时间等
select localtimestamp,DBTIMEZONE,current_date,sessiontimezone from dual;
计算当前日期时间的小时,分,秒,毫秒
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
)
当前sysdate的时分秒转化
SQL> DECLARE
2 DateValue date;
3 begin
4 select sysdate into DateValue from dual;
5 dbms_output.put_line('源时间:'||to_char(DateValue));
6 dbms_output.put_line('源时间减1天:'||to_char(DateValue-1));
7 dbms_output.put_line('源时间减1天1小时:'||to_char(DateValue-1-1/24));
8 dbms_output.put_line('源时间减1天1小时1分:'||to_char(DateValue-1-1/24-1/(24*60)));
9 dbms_output.put_line('源时间减1天1小时1分1秒:'||to_char(DateValue-1-1/24-1/(24*60)-1/(24*60*60)));
10 end;
11 /
源时间:05-NOV-10
源时间减1天:04-NOV-10
源时间减1天1小时:04-NOV-10
源时间减1天1小时1分:04-NOV-10
源时间减1天1小时1分1秒:04-NOV-10
PL/SQL procedure successfully completed
--转自