Solved

SQL Server Replace - Character Strip

Posted on 2011-09-06
4
332 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 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

22 Experts available now in Live!

Get 1:1 Help Now