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


IsAlphaNumeric


IsAlphaNumeric

Author
Message
Jon McGhee
Jon McGhee
SSC-Enthusiastic
SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)

Group: General Forum Members
Points: 165 Visits: 597
Comments posted to this topic are about the item IsAlphaNumeric
nhaberl
nhaberl
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 244
Wonderful - but where is the script?
WadeN 75975
WadeN 75975
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 117
Whoops...no script.
Jon McGhee
Jon McGhee
SSC-Enthusiastic
SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)

Group: General Forum Members
Points: 165 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.
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3175 Visits: 2907
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.
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3175 Visits: 2907
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
Old Hand
Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)

Group: General Forum Members
Points: 362 Visits: 130
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.
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3175 Visits: 2907
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
Old Hand
Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)

Group: General Forum Members
Points: 362 Visits: 130
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-Enthusiastic
SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)

Group: General Forum Members
Points: 165 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