Solved

SQL Server Replace - Character Strip

Posted on 2011-09-06
4
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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