Solved

Trim non alphanumeric characters but leave whitespace alon

Posted on 2013-10-30
2
283 Views
Last Modified: 2013-10-30
Hello I'm using the following function and call to remove non alphanumeric characters from a string.

It does do this but also removes white space.

I need to remove all non alphanumeric characters while leaving white space for our product title field of our products table.

How can I adjust my function to leave the white space?

Create Function [dbo].[RemoveNonAlphaCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin

    Declare @KeepValues as varchar(50) = '%[^a-z0-9]%'
    While PatIndex(@KeepValues, @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')

    Return @Temp
End

Open in new window




Select dbo.RemoveNonAlphaCharacters('This and that-:(_)')

returns

Thisandthat
0
Comment
Question by:gogetsome
2 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 39612337
Adding a space in the square brackets works in SQL 2K8 R2

Declare @Temp varchar(100) = 'This and that-:(_)'
Declare @KeepValues as varchar(50) = '%[^a-z0-9 ]%'

PRINT @Temp

While PatIndex(@KeepValues, @Temp) > 0
    Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')

PRINT @Temp

Open in new window

0
 

Author Closing Comment

by:gogetsome
ID: 39612347
Awesome! That was a easy fix! Appreciate your time.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question