一、加減
date型別可以直接加減天數,加減月份要勇add_months();
select e.hiredate + 30/24/60/60 + 20/24/60 + 5/24 as hiredate from emp e where rownum <= 1; 1980/12/17 5:20:30
select e.hiredate + 30/24/60/60 + 20/24/60 as hiredate from emp e where rownum <= 1; 1980/12/17 0:20:30
select e.hiredate + 30/24/60/60 as hiredate from emp e where rownum <= 1; 1980/12/17 0:00:30
select v.hiredate,
to_char(v.hiredate,'hh24') 時字元, --05
to_number(to_char(v.hiredate,'hh24')) 時數字, --5
to_char(v.hiredate, 'mi') 分字元, --20
to_number(to_char(v.hiredate, 'mi')) 分數字, --20
to_char(v.hiredate, 'ss') 秒字元, --30
to_number(to_char(v.hiredate, 'ss')) 秒數字,--30
to_char(v.hiredate, 'dd') 日字元, --17
to_number(to_char(v.hiredate, 'dd')) 日數字,--17
to_char(v.hiredate, 'mm') 月字元, --12
to_number(to_char(v.hiredate, 'mm')) 月數字,--12
to_char(v.hiredate, 'yyyy') 年字元, --1980
to_number(to_char(v.hiredate, 'yyyy')) 年數字,--1980
trunc(v.hiredate, 'yy') 年初, --1980/1/1
trunc(v.hiredate, 'day') 周初, --1980/12/14
trunc(v.hiredate, 'month') 月初, --1980/12/1
trunc(v.hiredate, 'dd') 一天之處, -- 1980/12/17
to_char(v.hiredate, 'day') 週幾, --星期三
to_char(v.hiredate, 'month') 月份, --12月
last_day(v.hiredate) 月末, --1980/12/31 5:20:30
add_months(trunc(v.hiredate, 'mm'), 1) 下月初 --1981/1/1
from (select e.hiredate + 30/24/60/60 + 20/24/60 + 5/24 as hiredate from emp e where rownum <= 1) v
例子:判斷一年是否是閏年
判斷一年是否是閏年,只看二月的月末是哪一天
select trunc(e.hiredate, 'yy') 年初 from emp e where e.empno = 7788 1987/1/1
select add_months(trunc(e.hiredate, 'yy'),1) 二月初 from emp e where e.empno = 7788 1987/2/1
select last_day(add_months(trunc(e.hiredate, 'yy'),1)) 二月底 from emp e where e.empno = 7788 1987/2/28
select to_char(last_day(add_months(trunc(e.hiredate, 'yy'),1)), 'dd') 二月最後一日 from emp e where e.empno = 7788 28
確定一年內屬於週內某一天的所有日期
要求返回指定年份的所有周五,
with x as(
select trunc(sysdate, 'y')+ (level -1) dy
from dual
connect by level <= add_months (trunc(sysdate, 'y'),12) - trunc(sysdate, 'y'))
select dy, to_char(dy, 'day') as 周五 from x where to_char(dy, 'd') = 6
列舉使用 connect by level
建立本月日曆
with x1 as
(select to_date('2013-06-1', 'yyyy-mm-dd') as cur_date from dual),
x2 as (select trunc(cur_date, 'mm') as 月初,
add_months(trunc(cur_date,'mm'), 1) as 下月初
from x1),
x3 as (select 月初 + (level - 1) as 日 from x2 connect by level <= (下月初 - 月初)),
x4 as (select to_char(日, 'iw') 所在周,
to_char(日, 'dd') 日期,
to_number(to_char(日, 'd')) 週幾
from x3)
select max(case 週幾 when 2 then 日期 end) 周一,
max(case 週幾 when 3 then 日期 end) 周二,
max(case 週幾 when 4 then 日期 end) 週三,
max(case 週幾 when 5 then 日期 end) 周四,
max(case 週幾 when 6 then 日期 end) 周五,
max(case 週幾 when 7 then 日期 end) 週六,
max(case 週幾 when 1 then 日期 end) 週日
from x4
group by 所在周
order by 所在周
with x as (select to_date('2013-06-1', 'yyyy-mm-dd') + (level - 1) as d
from dual
connect by level <= 5)
select d, to_char(d, 'day') as day, to_char(d, 'iw') as iw from x
Oracle關於時間的各種處理
v base time0 date v base time date v starttime number 10 begin v base time0 to date 1970 01 01 00 00 00 yyyy mm dd hh24 mi ss v base time to date to c...
關於oracle的時間檢索問題
select d.name,d.id,select count 1 from cds002 message me where d.id me.doc id and me.createdate like to date 2015 7 8 yyyy mm dd cnum,select count 1 f...
關於ORACLE時間儲存的問題
公司用oracle的資料庫,以前直接存實體的時候沒出現時間轉換問題。昨天,手寫sql語句 不是hql 時,傳date型別的資料給時間列,oracle報錯,不能識別,語句無效。解決方法共享 方法一 把date型別的資料,用 dateformat將date型別轉換成string 型別 dateforma...