Solved

SQL Server Query PatIndex Question

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

I have a function as shown here that is stripping out any non alphanumeric characters except the plus (+) sign.  I run an update statement against the whole table currently where I say for example:

Update Customer Set CustomerNumber = dboKeepNonAlphaCharacters(CustomerNumber)

The problem is this table has millions of rows so the logs grow when doing this and performance is slowed.  How can I only update those records that have a non alphanumberic number like shown below to only update that subset of records to keep the log down?


Alter Function [dbo].[KeepNonAlphaCharacters](@Temp VarChar(35)) Returns VarChar(35)
 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

0
Comment
Question by:sbornstein2
  • 2
3 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36493410
Add a WHERE clause. Try with:
your_column_to_clean LIKE '%[^a-zA-Z0-9+]%'

You can also do it with the PATINDEX(), but I would try it with the LIKE.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 36493419
In other words, like this:

UPDATE your_table
SET your_column_to_clean = dbo.KeepNonAlphaCharacters(your_column_to_clean)
WHERE your_column_to_clean  LIKE '%[^a-zA-Z0-9+]%'

Open in new window

0
 

Author Closing Comment

by:sbornstein2
ID: 36493443
cool thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.
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…

685 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