做数据迁移,一个字段的代码变了
要求71以后的是前面加D,比如71就是D71,71以前的是A开头的,并且是第一位是0的要去掉,比如01 就是A1
并且如果是0开头去掉0多于2位的,还要在第一位后加. 比如 0301 就是A3.01 不是0开头的就在第二位后加.
比如1103 就是A11.03(直接是11的除外,就是A11)  
这样的SQL怎么写呢,是不是要养到CASE函数?
原来存的是
01 预防保健科
02 全科医疗科
03 内科
0301 呼吸内科专业
0302 消化内科专业
0303 神经内科专业
0304 心血管内科专业
0305 血液内科专业
0306 肾病学专业
0307 内分泌专业
0308 免疫学专业
0309 变态反应专业
0310 老年病专业
0311 其他
。。
11 耳鼻咽喉科
1101 耳科专业
1102 鼻科专业
1103 咽喉科专业
1104 其他

71 护理部
72 药剂科
73 感染科
81 办公室
82 人事科
83 财务科
84 设备科
85 信息科(中心)
86 医政科
87 教育培训科
88 总务科
99 其他科室

解决方案 »

  1.   

    分几个Sql语句好了
    update table set bm='D'+bm where bm>='70'
    update table set bm=left(bm,2)+'.'+right(bm,len(bm)-2) where len(bm)>2 and left(bm,1)='0'
    update table set bm='A'+right(bm,len(bm)-1) where left(bm,1)='0'
      

  2.   

    分开写是最省事的,如果你非要一个语句搞定的话可以
    比如说你的表叫做table2,第一个字段叫做id
    用下面的去更新吧.
    update table2 t1
       set t1.id = (select id
                      from (select decode(substr(id, 1, 1),
                                          0,
                                          'A' || substr(id, 2, length(id)),
                                          id) id,
                                   id old_id
                              from table2
                             where to_number(id) < 71
                            union all
                            select 'D' || id id, id old_id
                              from table2
                             where to_number(id) >= 71
                               and length(id) < 4
                            union all
                            select 'A' || substr(id, 2, 1) || '.' ||
                                   substr(id, 3, length(id)) id,
                                   id old_id
                              from table2
                             where length(id) > 3
                               and id like '0%'
                            union all
                            select 'A' || substr(id, 1, 2) || '.' ||
                                   substr(id, 3, length(id)) id,
                                   id old_id
                              from table2
                             where length(id) > 3
                               and id not like '0%'
                             order by old_id) t2
                     where t1.id = t2.old_id)