SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Identify Bad Characters in a Table Column


Identify Bad Characters in a Table Column

Author
Message
dwain.c
dwain.c
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35975 Visits: 6431
Comments posted to this topic are about the item Identify Bad Characters in a Table Column


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)

Group: General Forum Members
Points: 81302 Visits: 20465
Interpreting the results from this isn't straightforward, at least not to me. I prefer the output formatted like this:

SELECT Strings, x.Goodchars, y.Badchars
FROM #t
CROSS APPLY (
SELECT SUBSTRING(Strings,n,1)
FROM (SELECT TOP (DATALENGTH(Strings)) n = ROW_NUMBER() OVER(ORDER BY @@SPID) FROM sys.columns a, sys.columns b) n
WHERE SUBSTRING(Strings,n,1) LIKE '%[a-zA-Z0-9]%'
FOR XML PATH('')
) x (Goodchars)
CROSS APPLY (
SELECT SUBSTRING(Strings,n,1)
FROM (SELECT TOP (DATALENGTH(Strings)) n = ROW_NUMBER() OVER(ORDER BY @@SPID) FROM sys.columns a, sys.columns b) n
WHERE SUBSTRING(Strings,n,1) LIKE '%[^a-zA-Z0-9]%'
FOR XML PATH('')
) y (Badchars)



Otherwise, an interesting article. Putting ^ into the @allowed string was a bit naughty ;-)

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
dwain.c
dwain.c
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35975 Visits: 6431
ChrisM@Work (5/8/2012)
Interpreting the results from this isn't straightforward, at least not to me. I prefer the output formatted like this:

SELECT Strings, x.Goodchars, y.Badchars
FROM #t
CROSS APPLY (
SELECT SUBSTRING(Strings,n,1)
FROM (SELECT TOP (DATALENGTH(Strings)) n = ROW_NUMBER() OVER(ORDER BY @@SPID) FROM sys.columns a, sys.columns b) n
WHERE SUBSTRING(Strings,n,1) LIKE '%[a-zA-Z0-9]%'
FOR XML PATH('')
) x (Goodchars)
CROSS APPLY (
SELECT SUBSTRING(Strings,n,1)
FROM (SELECT TOP (DATALENGTH(Strings)) n = ROW_NUMBER() OVER(ORDER BY @@SPID) FROM sys.columns a, sys.columns b) n
WHERE SUBSTRING(Strings,n,1) LIKE '%[^a-zA-Z0-9]%'
FOR XML PATH('')
) y (Badchars)



Otherwise, an interesting article. Putting ^ into the @allowed string was a bit naughty ;-)


Hey! To each his own. There's probably 20 or more ways to do this one and probably many that perform better. Your suggestion is also good.

Personally, I love playing with recursive CTEs, almost as much as I like being naughty, just to figure out how they work. :-P

I always hear people saying "stay away from recursive CTEs because they're slow" so I like to fall back on a classical education:


“If you know the enemy and know yourself, you need not fear the result of a hundred battles. If you know yourself but not the enemy, for every victory gained you will also suffer a defeat. If you know neither the enemy nor yourself, you will succumb in every battle”

― Sun Tzu, The Art of War, Special Edition


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)

Group: General Forum Members
Points: 81302 Visits: 20465
dwain.c (5/9/2012)
[quote][b]...Personally, I love playing with recursive CTEs, almost as much as I like being naughty, just to figure out how they work. :-P

I always hear people saying "stay away from recursive CTEs because they're slow" ...


Play is an excellent way to learn - I love to play with the problems folks post on here (far too little time these days). Here's a few examples of rCTE's to whet your appetite:

Paul White's Super-fast DISTINCT

Calculating interest rate

Handshaking

Running total with a twist

Sequence-numbering groups

Calculate volume percentages

Have fun!

Cheers

ChrisM

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
stephen.sarre
stephen.sarre
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 Visits: 297
-- Along the same lines, this is a very simple way of listing invalid data (allowing -_,;@' in the email column)

DECLARE @match VARCHAR(30)

SET @match = '%[^a-zA-Z0-9@.,;!-!_!'''']%'

SELECT email
FROM tbl_Staff
WHERE email IS NOT NULL
AND email LIKE @match ESCAPE '!'
ORDER BY email
dwain.c
dwain.c
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35975 Visits: 6431
stephen.sarre (5/10/2012)
-- Along the same lines, this is a very simple way of listing invalid data (allowing -_,;@' in the email column)

DECLARE @match VARCHAR(30)

SET @match = '%[^a-zA-Z0-9@.,;!-!_!'''']%'

SELECT email
FROM tbl_Staff
WHERE email IS NOT NULL
AND email LIKE @match ESCAPE '!'
ORDER BY email


Yep - that's a bit of a simpler problem.

In my case, I built the CTE to INSERT those bad characters into a temporary table, which I then used to construct a dynamic SQL UPDATE that replaced all the bad characters with ''. The second part ran really, really fast.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Iwas Bornready
Iwas Bornready
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34790 Visits: 886
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search