Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Allow Only Numbers Expand / Collapse
Author
Message
Posted Sunday, June 01, 2008 3:09 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, February 04, 2009 9:34 AM
Points: 72, Visits: 10
Comments posted to this topic are about the item Allow Only Numbers
Post #509558
Posted Sunday, June 01, 2008 9:48 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:03 PM
Points: 35,956, Visits: 30,244
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."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #509637
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse