Saturday, July 7, 2012

Check IsAlphaNumeric In SQL Server

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

No comments:

Post a Comment