oracle日期函數部分用法
日期和字符轉換函數用法(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; //獲取時間的秒
求某天是星期幾
SQL> select to_char(to_date('2015-10-27','yyyy-mm-dd'),'day') from dual;
TO_CHA
------
星期二
兩個日期間的天數
SQL> select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
FLOOR(SYSDATE-TO_DATE('20020405','YYYYMMDD'))
---------------------------------------------
4949
年月日的處理
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
)
)
處理月份天數不定的辦法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual