select bh ,bt from table编号字段
承2000-12
承2000-2
承2000-4
承2000-112
承2008-212写sql语句,要求把编号字段转化为
承2000-012
承2000-002
承2000-004
承2000-112
承2008-212

解决方案 »

  1.   


    --参照:SQL> select substr('承2000-2',1,6)|| lpad(substr('承2000-2',7),3,0) from dual;
      

  2.   

    select substr(bh,1,6)||lpad(substr(bh,7),3,'0'),bt from table
      

  3.   

    select bh ||substr(bt,1,6)|| lpad(substr(bt,3,0) from Table_test;
      

  4.   


    --修改传入你的字段吧
    SQL> select case when (9-length('承2000-2'))=1 then replace('承2000-2','-','-0')  2     when (9-length('承2000-2'))=2 then replace('承2000-2','-','-00')
      3            else '承2000-2' end
      4     from dual
      5  ;CASEWHEN(9
    ----------
    承2000-002SQL>
      

  5.   

    update table set bh=substr(bh,1,6)||lpad(substr(bh,7),3,'0')
      

  6.   

    select bh ||substr(bt,1,6)|| lpad(substr(bt,3,0) from Table_test;
      

  7.   

    今天刚弄了下正则,试试scott@ORA1> select * from test;BH
    --------------------------------------------------
    承2000-12
    承2000-2
    承2000-4
    承2000-112
    承2008-212scott@ORA1> SELECT
      2   regexp_replace(bh, '(.+)-(\d+)', '\1-') ||
      3   lpad(regexp_replace(bh, '(.+)-(\d+)', '\2'), 3, '0')
      4    FROM test;REGEXP_REPLACE(BH,'(.+)-(\D+)','\1-')||LPAD(REGEXP_REPLACE(BH,'(.+)-(\D+)','\2'),3,'0')
    ------------------------------------------------------------------------------------------------------------------------
    承2000-012
    承2000-002
    承2000-004
    承2000-112
    承2008-212
      

  8.   

    SQL> select substr('MyHoliday-ya',1,10)||lpad(substr('MyHoliday-ya',11),4,0) from dual 
      2  /SUBSTR('MYHOLIDAY-YA',1,10)||L
    ------------------------------
    MyHoliday-00ya