我有一张表A,表A里面有个一上万条记录,表A里每条记录都有一个字段名为到期期限字段,类型为日期型。格式为YYYYMMDD
如:20100914、20101017、20110403、20110808.......等等
假如今天日期是20100818那么我要写以下几个SQL语句筛选出我需要的记录。
第一个SQL:查询出还有1天要到期的记录。
第一个SQL:查询出2-30天要到期的记录。
第一个SQL:查询出2-30天要到期的记录。
第一个SQL:查询出1-3个月要到期的记录。
第一个SQL:查询出3-6个月要到期的记录。
第一个SQL:查询出6-12个月要到期的记录。
第一个SQL:查询出1年以上才要到期的记录。怎么写,请高手指教、

解决方案 »

  1.   

    --一天要到期的记录..其它相似,在1做变换,不知道你的月是如何定义的,30天一月?还是月相减?
    select * from tab
    where 到期期限字段-sysdate =1
      

  2.   

    按天查询,相差一天
    select * from A where 到期期限字段-to_char(sysdate,'YYYYMMDD') = 1
    按月查询,相差一月
    select * from A where substr(到期期限字段,0,6)-to_char(sysdate,'YYYYMM') = 1
    按年查询,相差一年
    select * from A where substr(到期期限字段,0,4)-to_char(sysdate,'YYYY') = 1
      

  3.   

    create table tb(qx varchar(10)) 
    insert into tb select '20100914' from dual;
    insert into tb select '20101017' from dual;
    insert into tb select '20110403'from dual;
    insert into tb select '20110808'from dual;select qx   from tb where  qx-to_char(sysdate,'YYYYMMDD') =1 ;
    select qx   from tb where  qx-to_char(sysdate,'YYYYMMDD')  between 2 and 30; select qx   from tb where  substr(qx,0,6)-to_char(sysdate,'YYYYMM') between 1 and 3;
    select qx   from tb where substr(qx,0,6)-to_char(sysdate,'YYYYMM') between 3 and 6;select qx   from tb where substr(qx,0,4)-to_char(sysdate,'YYYY') between 3 and 6;