日期:2014-05-17  浏览次数:20704 次

求助,oracle修改语句
HTML code
现有以下数据:
名称(nam)    编码(cod)
---------------------------
公司总部             01
 行政中心    0101
  行政部    010101
  法务部    010102
  项目部    010103  
  人事部    010104  
 财务中心    0102
  会计核算部    010201
  财务管理部    010202
  资金管理部    010203  
 
如果要修改"财务中心"编码"0102"为"0103",而下级部门编码也相应批量修改为"010301","010302","010303",
那么oracle修改语句该怎样写?
update hrm_organ set cod='?????' where cod like '0102%';


------解决方案--------------------
把字符串拿来组装就行了:
Update hrm_organ 
Set cod='0103' + substr(cod, 5)
Where cod like '0102%';


substr( string, start_position, [ length ] )
——取子字符串,从start_position开始,取length个,length为可选,如果length为空则返回start_position后的所有字符。
——start_position为负数时,表示从字符串尾巴倒着数。
------解决方案--------------------
update hrm_organ set cod='0103'||substr(cod,5) where cod like '0102%';
------解决方案--------------------
have a try
SQL code
update hrm_organ 
   set cod='0103' | (case when length(cod) > 4 then substr(cod, 5) else '' end) 
 where cod like '0102%'