How do I remove characters =,?, ,- and @ from a text column, but only if they appear as the prefix in a string?
For example:
| Input | Output |
|---|---|
#Stackoverflow |
Stackoverflow |
@#StackOverflow |
Stackoverflow |
=? -Stackoverflow |
Stackoverflow |
CodePudding user response:
Making the assumption "prefix" means to remove the characters to the left of the first non-special character, but special characters later in the string should remain:
with data as (
select '@#StackOverflow' string union all
select '=? Stack#@Overflow' union all
select 'Stack#@Overflow'
)
select * , Stuff(string,1,p.pos-1,'')
from data
cross apply(values(PatIndex('%[^=? -@#]%',string)))p(pos);
| Original | Fixed |
|---|---|
| @#StackOverflow | StackOverflow |
| =? Stack#@Overflow | Stack#@Overflow |
| Stack#@Overflow | Stack#@Overflow |
