-- 获取24小时
select to_char(to_date('20150202', 'yyyy-mm-dd hh24:mi:ss') +
(rownum - 1) / 24,
'hh24')
from dual
connect by rownum <= 24
--获取当前月的天
SELECT TO_CHAR(TO_DATE('201502', 'YYYYMM') + (ROWNUM - 1), 'YYYYMMDD') YM
FROM DUAL
CONNECT BY ROWNUM <=
LAST_DAY(TO_DATE('201502', 'YYYYMM')) - TO_DATE('201502', 'YYYYMM') + 1
--修改为如下的:
select to_date('202005','yyyymm')+(rownum-1) s_date from dual
connect by rownum<=last_day(to_date('202005','yyyymm')) - to_date('202005','yyyymm') + 1
-- 近30天
SELECT TO_CHAR(TO_DATE('20151210','yyyymmdd')-(ROWNUM-1), 'yyyymmdd') YM
FROM DUAL CONNECT BY ROWNUM < 31
-- 12个月
SELECT TO_CHAR('2015') || TRIM(TO_CHAR(ROWNUM, '09')) YM
FROM DUAL
CONNECT BY ROWNUM < 13;
SELECT to_char(SYSDATE,'yyyy-')||trim(to_char(ROWNUM,'09')) mm FROM dual CONNECT BY ROWNUM<13;
--查询近6个月
法一:
SELECT to_char(add_months(sysdate,-ROWNUM+1), 'yyyymm') YM
FROM DUAL CONNECT BY ROWNUM < 7
法二:
select to_char(add_months(sysdate,-5), 'yyyymm') YM from dual
union all
select to_char(add_months(sysdate,-4), 'yyyymm') YM from dual
union all
select to_char(add_months(sysdate,-3), 'yyyymm') YM from dual
union all
select to_char(add_months(sysdate,-2), 'yyyymm') YM from dual
union all
select to_char(add_months(sysdate,-1), 'yyyymm') YM from dual
union all
select to_char(add_months(sysdate,0), 'yyyymm') YM from dual
--查询当前日期之前的本月的天
SELECT TO_CHAR(TRUNC(SYSDATE, 'MONTH') + LEVEL - 1,
'YYYYMMDD') stat_cycle,
'01' dim_val_code1
FROM DUAL
CONNECT BY LEVEL <= TO_CHAR(SYSDATE-1, 'DD')
---查询一年中每一天是星期几
select to_date(2008 || '0101', 'yyyymmdd') + rownum - 1 rq,
to_char(to_date(2008 || '0101', 'yyyymmdd') + rownum - 1, 'day') day
from (select rownum
from dual
connect by rownum <= to_date(2008 || '1231', 'yyyymmdd') -
to_date(2008 || '0101', 'yyyymmdd') + 1);
--伪列 也可以用rownum
select level from dual connect by level <= 10 ;
select level from dual connect by 1 = 1;