Thanks Visakh !
The query is not giving the correct expected results
DECLARE @Masterip table(
Masterip Varchar(100) NOT NULL,
status varchar(20)
);
insert into @masterip
select
'5.46.200.1.462222.2.1.1.1.1.1',NULL
union all
select
'5.46.200.1.462222.2.1.1.1.1.1.1',NULL
union all
select
'5.46.200.1.462222.2.1.1.1.1.1.2',NULL
union all
select
'5.46.200.1.462222.2.1.1.1.1.1.3',NULL
union all
select
'5.46.200.1.462222.2.1.1.1.1.1.4',NULL
union all
select
'5.46.200.1.462222.2.1.1.1.1.5',NULL
union all
select
'5.46.200.1.462222.2.1.1.1.1.5.1',NULL
union all
select
'5.46.200.1.462222.2.1.1.1.1.6',NULL
UPDATE t
SET t.status= CASE WHEN Cnt > 0 THEN 'A' ELSE 'B' END
FROM @Masterip t
OUTER APPLY (SELECT COUNT(1) AS Cnt
WHERE Masterip LIKE t.Masterip + '.%'
AND Masterip <> t.Masterip
)t1
select * from @Masterip
Query results:
Masterip Status
----------------------------- ---
5.46.200.1.462222.2.1.1.1.1.1 B
5.46.200.1.462222.2.1.1.1.1.1.1 B
5.46.200.1.462222.2.1.1.1.1.1.2 B
5.46.200.1.462222.2.1.1.1.1.1.3 B
5.46.200.1.462222.2.1.1.1.1.1.4 B
5.46.200.1.462222.2.1.1.1.1.5 B
5.46.200.1.462222.2.1.1.1.1.5.1 B
5.46.200.1.462222.2.1.1.1.1.6 B
Expected Results:
Masterip Status
----------------------------- ----
5.46.200.1.462222.2.1.1.1.1.1 A
5.46.200.1.462222.2.1.1.1.1.1.1 B
5.46.200.1.462222.2.1.1.1.1.1.2 B
5.46.200.1.462222.2.1.1.1.1.1.3 B
5.46.200.1.462222.2.1.1.1.1.1.4 B
5.46.200.1.462222.2.1.1.1.1.5 A
5.46.200.1.462222.2.1.1.1.1.5.1 B
5.46.200.1.462222.2.1.1.1.1.6 A