首页 > 代码库 > 标量值函数—— 判断是否存在特殊字符

标量值函数—— 判断是否存在特殊字符

Create    FUNCTION [dbo].[IsOnlyNumAndEngth] --只允许数字,字幕和一个额外的特殊字符
    (
      @str VARCHAR(8000),
      @escape1 CHAR = ‘‘ --额外的特殊字符
    )
RETURNS BIT
AS 
    BEGIN 
        DECLARE @i INT 
        DECLARE @j INT 
        DECLARE @k BIT 
        DECLARE @g CHAR
        SET @i = DATALENGTH(@str) 
        SET @j = 1 
        SET @k = 1 
        IF @str = ‘‘ 
            BEGIN
                SET @k = 0 
            END
        WHILE @j <= @i 
            BEGIN 
                SET @g = SUBSTRING(@str, @j, 1)
                IF ( ASCII(@g) NOT IN ( ASCII(1), ASCII(2), ASCII(3),
                                        ASCII(4), ASCII(5), ASCII(6),
                                        ASCII(7), ASCII(8), ASCII(9),
                                        ASCII(0), ASCII(Q), ASCII(W),
                                        ASCII(E), ASCII(R), ASCII(T),
                                        ASCII(Y), ASCII(U), ASCII(I),
                                        ASCII(O), ASCII(P), ASCII(A),
                                        ASCII(S), ASCII(D), ASCII(F),
                                        ASCII(G), ASCII(H), ASCII(J),
                                        ASCII(K), ASCII(L), ASCII(Z),
                                        ASCII(X), ASCII(C), ASCII(V),
                                        ASCII(B), ASCII(N), ASCII(M),
                                        ASCII(q), ASCII(w), ASCII(e),
                                        ASCII(r), ASCII(t), ASCII(y),
                                        ASCII(u), ASCII(i), ASCII(o),
                                        ASCII(p), ASCII(a), ASCII(s),
                                        ASCII(d), ASCII(f), ASCII(g),
                                        ASCII(h), ASCII(j), ASCII(k),
                                        ASCII(l), ASCII(z), ASCII(x),
                                        ASCII(c), ASCII(v), ASCII(b),
                                        ASCII(n), ASCII(m),
                                        ASCII(@escape1) ) ) 
                    BEGIN 
                        SET @k = 0 
                        BREAK 
                    END
                SET @j = @j + 1 
            END 
        RETURN   @k 
    END

 

标量值函数—— 判断是否存在特殊字符