--5.17 時間和日期處理函數
select * from products;
--檢索年份是2007的所有 prod_end數據
select * from products
where to_number(to_char(prod_end,'YYYY'))= 2007;
select * from products
where prod_end between to_date('20060708','yyyy-mm-dd')
and to_date('20080708','yyyy-mm-dd');
--求某天是星期幾
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;
--兩個日期間的天數
select floor(sysdate - to_date('20080516','yyyymmdd')) from dual;
select ceil(to_date('20080518101812','yyyymmdd hh:mi:ss') - to_date('20080516101815','yyyymmdd hh:mi:ss')) from dual;--2 天花板函數,即加入正小數至最近整數
select floor(to_date('20080518101812','yyyymmdd hh:mi:ss') - to_date('20080516101815','yyyymmdd hh:mi:ss')) from dual;--1 地板函數,即舍去正小數至最近整數
--查找2008-05-03至2008-05-12間除星期一和七的天數
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2008-05-12','yyyy-mm-dd') - to_date('2008-05-03','yyyy-mm-dd')+1)
where to_char( to_date('2008-05-03','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )
--獲取05-03-2008至06-02-2008間有多少個月,如果一個月以下的,返回小數,如這個例子返回:0.967741935483871
select months_between(to_date('06-02-2008','MM-DD-YYYY'),
to_date('05-03-2008','MM-DD-YYYY')) "MONTHS" FROM DUAL;
--獲取小時,分鐘,秒,年月日
select sysdate ,to_char(sysdate,'hh') from dual;
select to_char('sdsf','sd') from dual;
--找出今年的天數
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
--閏年的處理方法
select to_char( last_day( to_date('02' || '2008','mmyyyy') ), 'dd' ) from dual
--一年的第幾天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
--是從當前開始下一個星期五的具體時間
select next_day(sysdate,6) from dual
--floor((date2-date1) /365) 作為年
--floor((date2-date1, 365) /30) 作為月
--mod(mod(date2-date1, 365), 30)作為日.
--to_char,函數功能,就是將數值型或者日期型轉化為字符型,FM :除空格
--9999999.0099:允許小數點左邊最大正數為7位,小數點右邊最少2位,最多4位,且在第5位進行四舍五入
select TO_CHAR(123.02,'FM9999999.00009') from DUAL
select TO_CHAR(0.123,'FM0.999') from DUAL