Remove the text between (branch)
CREATE TABLE #SampleData( SomeText varchar(1000)) INSERT #SampleData SELECT '( Branch is not CA and Branch is not MX)' UNION ALL SELECT '( Branch is not CA and Branch is not MX and Branch is not US)' UNION ALL SELECT '( Location North City) and ( Branch is not CA and Branch is not MX and Branch is not US)' UNION ALL SELECT '( Branch is not CA and Branch is not MX) and ( Location North City) ' UNION ALL SELECT '( Branch is not CA and Branch is not MX and Branch is not US) and ( MoreSales is Black or MoreSales is Red)' UNION ALL SELECT '( MoreSales is Black or MoreSales is Red) and ( Location North City or Location Down Town )'; select SomeText from #SampleData declare @ch varchar(200) declare @result varchar(200) set @ch = '( Branch is not CA and Branch is not MX)' set @ch = '( Branch is not CA and Branch is not MX and Branch is not US)' set @ch = '( Location North City)' set @ch = '( Location DownTown) and ( Branch is not CA and Branch is not MX and Branch is not US)' set @ch = '( Branch is not CA and Branch is not MX) and ( Location North City) ' set @ch = '( Branch is not CA and Branch is not MX and Branch is not US) and ( MoreSales is Black or MoreSales is Red)' set @ch = '( MoreSales is Black or MoreSales is Red) and (Location North City or Location Down Town )' BEGIN set @result = LTRIM(RTRIM(CAST(@ch as varchar(max)))) set @result = LEFT(@result, CASE WHEN PATINDEX('%Branch is not %',@result) > 0 THEN PATINDEX('%Branch is not %',@result) ELSE PATINDEX('%Branch is %',@result) END ) END select @result DROP TABLe #SampleData =========================== EXPECTED RESULT -- --- Display '' If no data other than country info --- Display full data If no country info --- Remove the values starting between '( Country )' --- Remove the unwanted 'and' =========================== '' '' '( Location North City)' '( Location DownTown)' '(Location North City) ' '( MoreSales is Black or MoreSales is Red)' '( MoreSales is Black or MoreSales is Red) and ( Location North City or Location Down Town )'
ShanmugaRaj