Solved

SQL Server Replace - Character Strip

Posted on 2011-09-06
4
328 Views
Last Modified: 2012-05-12
Hello all,

I need to update a field in a database table and strip out all characters except:
- Upper and Lower case alphanumeric are fine aA1
- The plus sign (+)

I want to strip all other characters from the field.

Thanks all
0
Comment
Question by:sbornstein2
  • 2
4 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36489174
Unfortunately, there is not a built-in regular expression replace in T-SQL, but here is an example using CLR if you are adventurous: http://weblogs.sqlteam.com/jeffs/archive/2007/04/27/SQL-2005-Regular-Expression-Replace.aspx

As an alternative, you could use a WHILE loop to check for existence of a value LIKE '%[^A-Za-z0-9+]%' and do iterative REPLACE() statements. Or using a numbers tables, especially if we are not dealing with large number of rows or long character strings, you could pull apart each character checking if they meet the regex above and if so substitute for ''. Then you can pull the string back together in character order.

I am about to go in transit, so can show an example; however, it will not be until later.
0
 

Author Comment

by:sbornstein2
ID: 36489295
What about a function that can do this?  I thought you could use regular expressions in SQL PatIndex.
0
 
LVL 8

Accepted Solution

by:
venk_r earned 250 total points
ID: 36489299
Try the below function and see if that helps.

ALTER Function [dbo].[KeepNonAlphaCharacters](@Temp VarChar(1000)) Returns VarChar(1000)
 AS
 Begin    
 While PatIndex('%[^a-zA-Z0-9+]%', @Temp) > 0   --AND PatIndex('+', @Temp) > 0  
 Set @Temp = Stuff(@Temp, PatIndex('%[^a-zA-Z0-9+]%', @Temp), 1, '')    
  Return @TEmp
 End
 
 Select dbo.KeepNonAlphaCharacters('Abc1234def5678ghi90jkl+;-')
0
 

Author Closing Comment

by:sbornstein2
ID: 36489328
perfect thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

12 Experts available now in Live!

Get 1:1 Help Now