Situatie
Mai jos este o functie ce primeste ca si parametru un string pe care il verifica daca este alfanumeric sau nu, returnand 1 daca este si 0 daca nu este.
Solutie
— select dbo.AlphaNumeric(‘AB123’) –returns 1
— select dbo.AlphaNumeric(‘A#123’) –returns 0
CREATE FUNCTION dbo.AlphaNumeric
(@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
end
end
return @isAN
END
Leave A Comment?