carlosm
carlosm

Reputation: 755

Remove specific characters from column

I would like to remove specific characters from a column using regexp.

As an example, I have this:

declare @a nvarchar(50) = '(djfhsd-kjfhksd'
select  Replace(@a, Substring(@a, PatIndex('%[^0-9.-]%', @a), 1), '')

But I want to remove just parenthesis (), spaces and dashes -
I don't have experience on regexp but I would like to remove them in one shot.

Upvotes: 1

Views: 86

Answers (1)

John Cappelletti
John Cappelletti

Reputation: 81960

declare @a nvarchar(50) = '(djfhsd-kjfhksd'
Select  @a =Replace(@a,RemChar,'')
  From  (Values ('('),
                (')'),
                ('-'),
                (' ')
            ) B (RemChar)

Select @a

Returns

djfhsdkjfhksd

For something straight forward. Otherwise you will need a UDF or a Cross Apply

Replace(Replace(Replace(Replace(YourCol,'(',''),')',''),'-',''),' ','')

Upvotes: 3

Related Questions