Hi @Harini ,
Thank you so much for posting here.
You could refer below and check whether it is working. Thanks.
drop table if exists Table1
drop table if exists Table2
drop table if exists Table3
create table Table1
(
Codes varchar(100)
)
insert into Table1 values
('KNC-GUSA-GEN-KWL-DSA-RLSA-PURCHASED-1234'),
('KNC-GUSA-GEN-KWL-DSA-RLSA-4563456'),
('KNC-GUSA-GEN-KWL-DSA'),
('KNC-GUK-GEN-KWL-RLSA-PUR-2134234'),
('KNC-GUK-GEN-KWL-RLSA-PR-12343'),
('KNC-GUK-GEN-KWL-RLSA-PUR')
create table Table2
(
Level0 varchar(100),
Level1 varchar(100)
)
insert into Table2 values
('KNC-GUK-GEN-KWL','KNC-GUK-GEN-KWL-RLSA'),
('KNC-GUSA-GEN-KWL-DSA','KNC-GUSA-GEN-KWL-DSA-RLSA')
create table Table3
(
Codes varchar(100),
Newcode varchar(100)
)
insert into Table3
select a.Codes,
case when a.Codes like b.Level1+'%' then b.Level1
when a.Codes like b.Level0+'%' and a.Codes not like b.Level1+'%' then b.Level0
end NewCode
from Table1 a, Table2 b
where a.Codes like b.Level0+'%'
select * from Table3
Output:
Codes Newcode
KNC-GUK-GEN-KWL-RLSA-PUR-2134234 KNC-GUK-GEN-KWL-RLSA
KNC-GUK-GEN-KWL-RLSA-PR-12343 KNC-GUK-GEN-KWL-RLSA
KNC-GUK-GEN-KWL-RLSA-PUR KNC-GUK-GEN-KWL-RLSA
KNC-GUSA-GEN-KWL-DSA-RLSA-PURCHASED-1234 KNC-GUSA-GEN-KWL-DSA-RLSA
KNC-GUSA-GEN-KWL-DSA-RLSA-4563456 KNC-GUSA-GEN-KWL-DSA-RLSA
KNC-GUSA-GEN-KWL-DSA KNC-GUSA-GEN-KWL-DSA
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.