用起始字符的条件替换属性的前一个或两个字符

Replacing first one or two characters of an attribute with conditions on the starting characters

select distinct a.OriginalAttribute,

CASE WHEN LEFT(a.OriginalAttribute, 1) ='0'  THEN 5||substr(a.OriginalAttribute,2)             

when LEFT(a.OriginalAttribute, 2) ='10' THEN 51||substr(a.OriginalAttribute,3) END AS NEWValue  

 ,b.attributeB, c.attributeC

from tablea a, tableb b, tablec c

where a.KEY = b.key and a.key = c.key;

错误: ORA-00904: “LEFT”: 无效标识符

错误解释: 顺便说一句,以下 sql 有效:这证实了我的“a.OriginalAttribute”是我的属性之一 table

select distinct a.OriginalAttribute,b.attributeB, c.attributeC

from tablea a, tableb b, tablec c

where a.KEY = b.key and a.key = c.key;

==> 我不明白为什么我把它放在左边的函数上 Oracle 找不到它。

想要的结果:

如果 a.OriginalAttribute 以“0”开头,将“0”替换为“5”(示例:004441 -> 504441

如果a.OriginalAttribute以“10”开头,将“10”替换为“51”(例如:104441 -> 514441

有人能帮帮我吗?

谢谢。

使用 substr 而不是 left。

select distinct a.OriginalAttribute,
CASE WHEN SUBSTR(a.OriginalAttribute, 1, 1) ='0'  
     THEN 5||substr(a.OriginalAttribute,2)             
     when SUBSTR(a.OriginalAttribute, 1 ,2) ='10' 
     THEN 51||substr(a.OriginalAttribute,3) END AS NEWValue  
 ,b.attributeB, c.attributeC
from tablea a, tableb b, tablec c

其中 a.KEY = b.key 和 a.key = c.key;

或者,看看这样的事情是否有帮助:

select distinct 
  a.OriginalAttribute,
  regexp_replace(regexp_replace(a.OriginalAttribute, '^0', '5'), '^10', '51') as NEWValue,
  b.attributeB, 
  c.attributeC
from tablea a join tableb b on a.key = b.key
join tablec c on a.key = c.key;

因为这个:

SQL> with test (col) as
  2    (select '004441' from dual union all
  3     select '104441' from dual union all
  4     select '312345' from dual
  5    )
  6  select col,
  7    regexp_replace(regexp_replace(col, '^0', '5'), '^10', '51') result
  8  from test;

COL    RESULT
------ --------------------
004441 504441
104441 514441
312345 312345

SQL>