例如:数据库里有一张表Guigance,内有三个字段:迟延日数(DELAYDAY)、实施年月日(EXECUTE_DATE)、预定年月日(PLAN_DATE)条件:如果实施年月日为NULL时,迟延日数=预定年月日-系统日期;否则,迟延日数=预定年月日-实施年月日。问题:请问,我怎么写SQL语句,才能搜出迟延日数(DELAYDAY)是符合以上要求的?

解决方案 »

  1.   

    select nvl2(delayday,plan_date - sysdate,plan_date - execute_date) from guigance最好能使日期格式一致.
      

  2.   

    改一下,将NVL2(DELAYDAY,....)改为NVL2(EXECUTE_DATE,...)
      

  3.   

    select delayday from guigance 
    where (execute_date is null and delayday=plan-sysdate) 
    or (execute_date is not null and delayday=plan-execute_date)
    要求:
    oracle 9i的环境,并且dalayday、execute_date、plan_date都必须是date型
    经过测试可以用的,一定要给分哦!o(∩_∩)o...
      

  4.   

    楼主要求:搜出迟延日数(DELAYDAY)是符合以上要求的
    而1楼给出的计算结果,所以3楼的回答正确。
    鉴定完毕!
      

  5.   

    select nvl2(execute_date,plan_date - execute_date,plan_date - sysdate) from guigance
      

  6.   

    我觉得楼主的问题好像有点不妥一样:系统日期随时会改变.你的延期天数能够在查询之前确定吗.如果不能确定,那又哪来的delayday=plan-sysdate  or  delayday=plan_date-execute_date这样的条件.
      

  7.   

    哦,我忘记一点.我用的这两个字段是8位number型的,如:20070707,在表字段不更改的情况下,请给出解决方法,
      

  8.   

    select   nvl2(delayday,plan_date-to_number(to_char(sysdate,'yyyymmdd')),plan_date-execute_date) as delayday  from   guigance
    日期型转为数字型就可以了!
      

  9.   

    Oracle 不是提供块吗?就和写程序一样,你先进行判断,在执行就可以了:)
      

  10.   

    也可以这样
    select  decode(delayday,
                   null,plan_date-to_number(to_char(sysdate,'yyyymmdd')),
                   plan_date-execute_date)   as   delayday     
    from    guigance 与8楼的类似^_^