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


Allow Only Numbers


Allow Only Numbers

Author
Message
Scott Williams
Scott Williams
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 10
Comments posted to this topic are about the item Allow Only Numbers
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (206K reputation)SSC Guru (206K reputation)SSC Guru (206K reputation)SSC Guru (206K reputation)SSC Guru (206K reputation)SSC Guru (206K reputation)SSC Guru (206K reputation)SSC Guru (206K reputation)

Group: General Forum Members
Points: 206829 Visits: 41961
Hi Scott...

You have some minor errors you need to fix... (found in 2k, and 2k5)

Msg 102, Level 15, State 1, Procedure fnc_AllowNumbersOnly, Line 8
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Procedure fnc_AllowNumbersOnly, Line 9
Incorrect syntax near ' '.


The cause of the errors is 4 hidden characters just before the DECLARE and the WHILE. Past the code into MS Word to see them.

Also, consider getting rid of the @lenPhone and @phoneStr variables since they're declared but not used anywhere.

If I may suggest, consider not using the WHILE loop at all... consider the Tally table which makes things run more than twice as fast when a fair number non-digit characters are present and still about 20% faster than when they are not.... Yeah, I know... I could use LIKE... but the OR, in this case, shaves an extra second off a 10,000 row run when there's more than just a couple of non-numeric digits...

 CREATE FUNCTION dbo.fnDigitsOnly
(@String NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
SELECT @String = STUFF(@String,N,1,'')
FROM dbo.Tally
WHERE N <= LEN(@String)
AND (SUBSTRING(@String,N,1) < '0'
OR
SUBSTRING(@String,N,1) > '9')
ORDER BY N DESC
RETURN @String
END



Tally table may be found here...
http://www.sqlservercentral.com/articles/TSQL/62867/

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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