'How do I remove certain leading characters from text
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 |
Solution 1:[1]
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 |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stu |
