问大家一个问题`!
请问如何根据 年月 条件.判断这个年月是属于上半年or 下半年,然后进行查询.
如:2009  05  ,查出2009 01-2009 06````的所有数据.请问要如何写呢?
 select * from dual 
 where```~~````

解决方案 »

  1.   

    上半年
    where to_char(date1,'mm')<='06'
    下半年.....>='06'
      

  2.   

    select to_char(sysdate,'mm') from dual;
      

  3.   

    你是要程序中实现,还是在数据库中实现?
    PL SQL:IF MONTH(SYSDATE)<7 THEN 
      select * From tab1 where month(col1) between 1 and 6  and year(col1)=year(sysdate);
    ELSE
      select * From tab1 where month(col1) between 7 and 12  and year(col1)=year(sysdate);
    END IF
      

  4.   


    SQL> select decode(round((to_char(sysdate,'mm')-6)/6,0),1,'下半年','上半年') from dual;DECODE(ROUND((TO_CHAR(SYSDATE,
    ------------------------------
    下半年SQL> 
      

  5.   

    看季度也可以SELECT DECODE(TO_CHAR(SYSDATE,'Q'),'1','上半年','2','上半年','下半年') FROM DUAL;
      

  6.   


    我的意思是`按照`年、月 
    如:2009  05  ,查出2009 01-2009 06的所有数据.如:2009  08  ,查出2009 07-2009 12的所有数据 是在where 后面写个判决来实现请问?

      

  7.   


    --试一下:
    select count(*) from your_tableA where to_char(your_date_field,'yyyymm')='200905';
      

  8.   

    直接把起始日期和终止日期给用户吧!select count(*) from your_tableA where to_char(your_date,'yyyymm')>=起始日期 and to_char(your_date,'yyyymm')<=终止日期;即:select count(*) from your_tableA where to_char(your_date,'yyyymm')>='200901' and to_char(your_date,'yyyymm')<='200906';
      

  9.   

    SQL> with tt as(select add_months(sysdate,-rownum) date1,rownum rn from dual
      2  connect by rownum<100)
      3  select * from tt
      4  where date1>=to_date(substr(:inputdate,1,4)||case when substr(:inputdate,5,2)<='06' then '01' else '07' end,'yyyymm')
      5  and date1<add_months(to_date(substr(:inputdate,1,4)||case when substr(:inputdate,5,2)<='06' then '06' else '12' end,'yyyymm'),1)
      6  ;
      

  10.   

    select * from tt
       where date1>=to_date(substr(:inputdate,1,4)||case when substr(:inputdate,5,2)<='06' then '01' else '07' end,'yyyymm')
       and date1<add_months(to_date(substr(:inputdate,1,4)||case when substr(:inputdate,5,2)<='06' then '06' else '12' end,'yyyymm'),1);:inputdate为你要查询的年月
      

  11.   

    with tt as(select add_months(sysdate,-rownum) date1,rownum rn from dual
       connect by rownum<100)
        select * from tt
        where date1>=to_date(substr(&inputdate,1,4)||case when substr(&inputdate,5,2)<='06' then '01' else '07' end,'yyyymm')
        and date1<add_months(to_date(substr(&inputdate,1,4)||case when substr(&inputdate,5,2)<='06' then '06' else '12' end,'yyyymm'),1)
        order by date1
        ;