Sql 在oracle中替换

Sql 在oracle中替换,sql,oracle,replace,case,Sql,Oracle,Replace,Case,我需要你的帮助来替换(在Oracle中)。 我需要在我的测试中替换一列中的多个字符。将5更改为“测试”可以。如果我想再次更改5和4,您有解决方案吗 如果要匹配整个值,我建议使用case表达式,该表达式可以轻松扩展 假设您想将4和5转换为“测试”: 或者,如果需要不同的值: case resource_state_key when 4 then 'TEST' when 5 then 'TEST2' end as val 在Oracle中,后者也可以使用特定于供应商的函数decod

我需要你的帮助来替换(在Oracle中)。 我需要在我的测试中替换一列中的多个字符。将5更改为“测试”可以。如果我想再次更改5和4,您有解决方案吗


如果要匹配整个值,我建议使用
case
表达式,该表达式可以轻松扩展

假设您想将
4
5
转换为“测试”:

或者,如果需要不同的值:

case resource_state_key
    when 4 then 'TEST'
    when 5 then 'TEST2'
end as val
在Oracle中,后者也可以使用特定于供应商的函数
decode()


如果要匹配整个值,我建议使用
case
表达式,该表达式可以轻松扩展

假设您想将
4
5
转换为“测试”:

或者,如果需要不同的值:

case resource_state_key
    when 4 then 'TEST'
    when 5 then 'TEST2'
end as val
在Oracle中,后者也可以使用特定于供应商的函数
decode()


<>我不认为你想做什么作为一个字符串操作。相反,看起来你想在一个引用表中查找一个值。你应该在数据库中有一个“资源状态”引用表。< /P> 如果数据库中没有,可以在查询中生成一个:

with resource_state_ref as (
      select 4 as resource_state_key, 'TEST' as val union all
      select 5 as resource_state_key, 'TEST' as val 
     )
select ref.val, t.*
from t left join
     resource_state_ref ref
     on t.resource_state_key = ref.resource_state_key

<>我不认为你想做什么作为一个字符串操作。相反,看起来你想在一个引用表中查找一个值。你应该在数据库中有一个“资源状态”引用表。< /P> 如果数据库中没有,可以在查询中生成一个:

with resource_state_ref as (
      select 4 as resource_state_key, 'TEST' as val union all
      select 5 as resource_state_key, 'TEST' as val 
     )
select ref.val, t.*
from t left join
     resource_state_ref ref
     on t.resource_state_key = ref.resource_state_key

请以文本(而非图像)的形式提供查询、数据和预期输出。请以文本(而非图像)的形式提供查询、数据和预期输出。为什么不
replace(replace(resource\u state\u key,4,'TEST'),5,'TEST')
?@TaaviTiitsu:OP似乎希望匹配整个输入值(大概是一个数字),因此
replace()
是不合适的。如果值是
45
,您的代码将返回
'TESTTEST'
,我想这不是OP想要的。另外,
case
如果有很多值要转码(而
replace()
需要嵌套函数调用),那么它的伸缩性更好。为什么不
replace(replace(resource_state_key,4,'TEST'),5,'TEST')
?@TaaviTiitsu:似乎OP想要匹配整个输入值(大概是一个数字),所以
replace()
不合适。如果值为
45
,该怎么办?您的代码将返回
'TESTTEST'
,我想这不是OP想要的。此外,
case
如果有很多值要转码(而
replace()
需要嵌套函数调用),则伸缩性更好。