在甲骨文中替换
REPLACE In oracle
我需要你的帮助以在 Oracle 中进行 REPLACE( 。
我需要在我的测试中替换一列中的许多字符,可以将 5 更改为 'test' 如果我想再次更改 5 和 4,您有解决方案吗?
如果你想匹配整个值,我会推荐一个 case
表达式,它可以很容易地扩展。
假设您要将 4
和 5
都转码为“TEST”:
case when resource_state_key in (4, 5) then 'TEST' end as val
或者如果您想要不同的值:
case resource_state_key
when 4 then 'TEST'
when 5 then 'TEST2'
end as val
在Oracle中,后者也可以用vendor-specific function表示decode()
:
decode(resource_state_key, 4, 'TEST', 5, 'TEST2') as val
我不认为你想做的是字符串操作。相反,您似乎想要在引用 table 中查找值。您应该在数据库中有一个“资源状态”引用 table。
如果数据库中没有,您可以在查询中生成一个:
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
我需要你的帮助以在 Oracle 中进行 REPLACE( 。 我需要在我的测试中替换一列中的许多字符,可以将 5 更改为 'test' 如果我想再次更改 5 和 4,您有解决方案吗?
如果你想匹配整个值,我会推荐一个 case
表达式,它可以很容易地扩展。
假设您要将 4
和 5
都转码为“TEST”:
case when resource_state_key in (4, 5) then 'TEST' end as val
或者如果您想要不同的值:
case resource_state_key
when 4 then 'TEST'
when 5 then 'TEST2'
end as val
在Oracle中,后者也可以用vendor-specific function表示decode()
:
decode(resource_state_key, 4, 'TEST', 5, 'TEST2') as val
我不认为你想做的是字符串操作。相反,您似乎想要在引用 table 中查找值。您应该在数据库中有一个“资源状态”引用 table。
如果数据库中没有,您可以在查询中生成一个:
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