Solved

SQL Server Query PatIndex Question

Posted on 2011-09-06
3
387 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore 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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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