Given the below string
Declare @string varchar(max)=\'abc ___________ deffns ___ cg _ hif _______hh ihs\';
this is the Output requi
First get rid of the multiple underscores, then do the replace.
Here is one method:
select replace(replace(replace(@string, '_', '><' ), '<>', '' ), '><', 'LASTNAME' )