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


IsAlphaNumeric


IsAlphaNumeric

Author
Message
Jon McGhee
Jon McGhee
SSC Veteran
SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)

Group: General Forum Members
Points: 219 Visits: 597
Comments posted to this topic are about the item IsAlphaNumeric
nhaberl
nhaberl
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 251
Wonderful - but where is the script?
WadeN 75975
WadeN 75975
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: 117
Whoops...no script.
Jon McGhee
Jon McGhee
SSC Veteran
SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)

Group: General Forum Members
Points: 219 Visits: 597
My bad. I've resubmitted it. The contribution editor is... confusing. In my defense the field for the sql text strips the white space and looks odd as it became one line of code. I've tried with explorer 9 and chrome 15. I originally put the code in the "Scripts Manager", at least now I know.
nigel.
nigel.
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4675 Visits: 2939
But it's still not there Jon ! w00t

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

nigel.
nigel.
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4675 Visits: 2939
Aha! I found where you hid it eventually :-D

There's no need for a loop! What's wrong with PATINDEX for this sort of thing?


SELECT PATINDEX('%[^A-Za-z0-9 ]%', 'ABC 123')

SELECT PATINDEX('%[^A-Za-z0-9 ]%', 'ABC*123')



--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

dUros
dUros
Mr or Mrs. 500
Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)

Group: General Forum Members
Points: 568 Visits: 131
I expected function like that. Is string numeric or not.


CREATE FUNCTION [dbo].[IsAlphaNumeric](
@input varchar(100))
RETURNS bit
AS
BEGIN

DECLARE @i int, @max int, @c varchar(1)
DECLARE @isAN bit

SET @max = LEN(@input)
SET @isAN= 1
SET @i = 0

WHILE @i < @max BEGIN

SET @i = @i + 1
SET @c = SUBSTRING(@input,@i,1)

IF PATINDEX('%[0-9]%', @c) = 0 BEGIN
SET @isAN = 0
BREAK
END

END

RETURN @isAN

END


nigel.
nigel.
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4675 Visits: 2939
dUros (12/2/2011)
I expected function like that. Is string numeric or not.



There's no need to use a while loop, it's unnecessary!

You can find non-matches in a string in one line with PATINDEX using the ^ to invert the search. See http://msdn.microsoft.com/en-us/library/ms188342.aspx

SELECT PATINDEX('%[^0-9]%', '123')

SELECT PATINDEX('%[^0-9]%', '123a')



--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

dUros
dUros
Mr or Mrs. 500
Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)

Group: General Forum Members
Points: 568 Visits: 131
You are true Nigel, thanks :-).

Here is now simple function:


ALTER FUNCTION [dbo].[IsAlphaNumeric](
@input varchar(100))
RETURNS bit
AS
BEGIN

DECLARE @isAN bit

SET @isAN= 0 --non numeric

IF PATINDEX('%[^0-9]%', @input) = 0 BEGIN
SET @isAN = 1 --numeric
END

RETURN @isAN

END



Or just test the returns of PATINDEX, ">0" or "=0" .
Jon McGhee
Jon McGhee
SSC Veteran
SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)

Group: General Forum Members
Points: 219 Visits: 597
Agreed. My only issue with PATINDEX is it's hard to read the code, but it is leaner code.
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