已知表中存在以下数据
Order_Auto       fact     
SL2009110001       SL
SL2009110002       SL
SL2009100001       SL
SL2009100003       SL
Order_Auto的组成是由 fact的值加上年月以及四位流水号,
如果当前是09年9月时, 要求得到 SL2009100001
如果当前是09年10月时,要求得到 SL2009100004
如果当前是09年11月时,要求得到 SL2009110003
请问用一条sql该如何实现!

解决方案 »

  1.   

    decode(to_char(time,'hh24'),'09','SL2009100001','10','SL2009100004','SL2009110003
    ')
      

  2.   

    fact的值只有SL这一种吗?
    fact的值会不会变化?
      

  3.   

    select :fact||to_char(sysdate,'yyyymm')||to_char(nvl(max(to_number(substr(Order_Auto,length(Order_Auto)-3)),0),'0000') Order_Auto
    from table1
    where Order_Auto like :fact||to_char(sysdate,'yyyymm')||'%'
      

  4.   

    如果fact的值都是一致的话可以这样:
    select nvl(max(substr(order_auto,1,8))||trim(to_char(max(substr(order_auto,9,4)+1),'0000')),'SL'||to_char(sysdate,'yyyymm')||'0001') result
    from tb where substr(order_auto,3,6) = to_char(sysdate,'yyyymm');
      

  5.   

    14:41:23 scott@TUNGKONG> select * from tb;ORDER_AUTO   FACT
    ------------ ----
    SL2009110001 SL
    SL2009110002 SL
    SL2009100001 SL
    SL2009100003 SL已用时间:  00: 00: 00.00
    14:41:24 scott@TUNGKONG> select nvl(max(substr(order_auto,1,8))||trim(to_char(max(substr(order_auto,9,4)+1),'0000')),'SL'||to_char(sysdate,'yyyymm')||'0001') result
    14:41:28   2  from tb where substr(order_auto,3,6) = to_char(sysdate,'yyyymm');RESULT
    ---------------------
    SL2009110003已用时间:  00: 00: 00.00
    14:41:29 scott@TUNGKONG> select nvl(max(substr(order_auto,1,8))||trim(to_char(max(substr(order_auto,9,4)+1),'0000')),'SL'||'&testdate'||'0001') result
    14:41:44   2  from tb where substr(order_auto,3,6) = '&testdate';
    输入 testdate 的值:  200909
    原值    1: select nvl(max(substr(order_auto,1,8))||trim(to_char(max(substr(order_auto,9,4)+1),'0000')),'SL'||'&testdate'||'0001') result
    新值    1: select nvl(max(substr(order_auto,1,8))||trim(to_char(max(substr(order_auto,9,4)+1),'0000')),'SL'||'200909'||'0001') result
    输入 testdate 的值:  200909
    原值    2: from tb where substr(order_auto,3,6) = '&testdate'
    新值    2: from tb where substr(order_auto,3,6) = '200909'RESULT
    ---------------------
    SL2009090001已用时间:  00: 00: 00.00
    14:41:52 scott@TUNGKONG> /
    输入 testdate 的值:  200910
    原值    1: select nvl(max(substr(order_auto,1,8))||trim(to_char(max(substr(order_auto,9,4)+1),'0000')),'SL'||'&testdate'||'0001') result
    新值    1: select nvl(max(substr(order_auto,1,8))||trim(to_char(max(substr(order_auto,9,4)+1),'0000')),'SL'||'200910'||'0001') result
    输入 testdate 的值:  200910
    原值    2: from tb where substr(order_auto,3,6) = '&testdate'
    新值    2: from tb where substr(order_auto,3,6) = '200910'RESULT
    ---------------------
    SL2009100004已用时间:  00: 00: 00.01