Technical Article

IsAlphaNumeric

,

select dbo.IsAlphaNumeric('ABC 123') --returns 1

select dbo.IsAlphaNumeric('ABC*123') --returns 0

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

declare @i int, @max int, @c varchar(1), @asc int

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)
    set @asc = ascii(@c)
    set @isAN = 
        case when @asc between 48 and 57 then 1 -- 0 9
        when @asc between 65 and 90 then 1 -- A Z
        when @asc between 97 and 122 then 1 -- a z
        when @asc = 32 then 1 --space
        else 0
        end
        
    if @isAN = 0 begin
        return @isAN --not alpha
    end 

end 
return @isAN -- is alpha

END

Rate

2.2 (5)

You rated this post out of 5. Change rating

Share

Share

Rate

2.2 (5)

You rated this post out of 5. Change rating