piliskys

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  25 随笔 :: 0 文章 :: 40 评论 :: 0 Trackbacks
问题:查出在2001年5年到2004年1月的所有年月格式为yyyymm(其实也是别人问到我这样一个相关的问题)
select to_char(to_date('200105','yyyymm'),'yyyy')+ b.bb||'00'+a.aa  ff
     
from 
    (
select level aa from dual connect by 1=1 and level <13 ) a, 
    (
select level-1 bb from dual connect by 1=1 and level <=
    (to_char(to_date(
'200402','yyyymm'),'yyyy'- to_char(to_date('200105','yyyymm'),'yyyy')+1)  ) b
    
where  to_char(to_date('200105','yyyymm'),'yyyy')+ b.bb||'00'+a.aa  >='200105'
    
and to_char(to_date('200105','yyyymm'),'yyyy')+ b.bb||'00'+a.aa  <='200402'

不用其它表,就可以构造出一个时间表来,注意:connect by据说只支持oracle 9i 及以上
posted on 2005-11-23 17:44 霹雳火 阅读(503) 评论(1)  编辑  收藏 所属分类: oracle数据库

评论

# re: connect by构建时间段表 2005-11-24 09:51 霹雳火
select * from (
select to_char(add_months(to_date('200501','yyyymm'), rownum), 'yyyymm') from dual
connect by rownum<months_between(to_date('200512','yyyymm'),to_date('200501','yyyymm')));
发现别人一种写法更好  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: