Remove special charactors T-sql

Hi I found the function i have to remove the special characters in the Table. But After i used that function all sentence with out spaces.
Original :  select dbo.RemoveSpecialChars('Test-Master," ®+ BAP Reagent')
Results after use function below - TestMasterBAPReagent
Expected Results Test-Master  BAP Reagent

 ALTER function [dbo].[RemoveSpecialChars] (@s varchar(256)) returns varchar(4000)
       with schemabinding
       if @s is null
          return null
       declare @s2 varchar(256)
       set @s2 = ''
       declare @l int
       set @l = len(@s)
       declare @p int
       set @p = 1
       while @p <= @l begin
          declare @c int
          set @c = ascii(substring(@s, @p, 1))
          if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122
             set @s2 = @s2 + char(@c) 
          set @p = @p + 1
       if len(@s2) = 0
          return null
       return @s2

Open in new window

Any idea?
LVL 10
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
The code section

if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122

Open in new window

Is determining which characters to keep.  If you want spaces, dashes, etc. included then just add those ASCII values.

if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122 or @c IN (32, 45)

Open in new window
Vikas GargAssociate Principal EngineerCommented:

Try this query

Declare @KeepValues as varchar(50),@Str varchar(100)

 SET @Str = 'Test-Master," ®+ BAP Reagent'

    Set @KeepValues = '%[^-A-Z ]%'
    While PatIndex(@KeepValues, @Str) > 0
    Set @Str = Stuff(@Str, PatIndex(@KeepValues, @Str), 1, '')


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
ukerandi, do you still need help with this question?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.