Solved

SQL Server Query PatIndex Question

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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