Create rule to restrict special characters in table in sql server

前端 未结 3 2018
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 11:29

I want to create a rule to restrict special characters to be entered into a column. I have tried the following. But it didnt work.

CREATE RULE rule_spchar
AS
@ma         


        
3条回答
  •  星月不相逢
    2021-02-09 12:10

    Your can create a Check Constraint on this column and only allow Numbersand Alphabets to be inserted in this column, see below:

    Check Constraint to only Allow Numbers & Alphabets

    ALTER TABLE Table_Name 
    ADD CONSTRAINT ck_No_Special_Characters 
           CHECK (Column_Name NOT LIKE '%[^A-Z0-9]%') 
    

    Check Constraint to only Allow Numbers

    ALTER TABLE Table_Name 
    ADD CONSTRAINT ck_Only_Numbers 
           CHECK (Column_Name NOT LIKE '%[^0-9]%') 
    

    Check Constraint to only Allow Alphabets

    ALTER TABLE Table_Name 
    ADD CONSTRAINT ck_Only_Alphabets 
           CHECK (Column_Name NOT LIKE '%[^A-Z]%') 
    

提交回复
热议问题