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 12»»

IsAlphaNumeric Expand / Collapse
Author
Message
Posted Thursday, December 1, 2011 12:09 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 12:27 PM
Points: 83, Visits: 597
Comments posted to this topic are about the item IsAlphaNumeric
Post #1214375
Posted Thursday, December 1, 2011 12:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 1, 2014 3:16 AM
Points: 9, Visits: 184
Wonderful - but where is the script?
Post #1214390
Posted Thursday, December 1, 2011 5:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 8:57 AM
Points: 41, Visits: 82
Whoops...no script.
Post #1214551
Posted Thursday, December 1, 2011 6:29 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 12:27 PM
Points: 83, 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.
Post #1214579
Posted Thursday, December 1, 2011 8:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:34 AM
Points: 1,174, Visits: 2,654
But it's still not there Jon !

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

Post #1214685
Posted Thursday, December 1, 2011 8:41 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:34 AM
Points: 1,174, Visits: 2,654
Aha! I found where you hid it eventually

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

Post #1214693
Posted Friday, December 2, 2011 1:11 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 29, 2014 3:13 AM
Points: 46, Visits: 104
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

Post #1215185
Posted Friday, December 2, 2011 2:00 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:34 AM
Points: 1,174, Visits: 2,654
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

Post #1215199
Posted Friday, December 2, 2011 5:57 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 29, 2014 3:13 AM
Points: 46, Visits: 104
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" .
Post #1215293
Posted Friday, December 2, 2011 6:32 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 12:27 PM
Points: 83, Visits: 597
Agreed. My only issue with PATINDEX is it's hard to read the code, but it is leaner code.
Post #1215302
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse