Solved

Trim non alphanumeric characters but leave whitespace alon

Posted on 2013-10-30
2
281 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now