Home > Blockchain >  How to avoid inserting special characters in a SQL Server column?
How to avoid inserting special characters in a SQL Server column?

Time:08-11

I need to create a restriction in SQL Server that prevents inserting special characters into the firstname column.

I have tried to create it this way:

ALTER TABLE [CTPlatform].[Users] WITH NOCHECK 
    ADD CONSTRAINT [CK_Users_Firstname] 
        CHECK ([FirstName] LIKE '[A-Z]')
GO

But it only allows me to enter one character, example: 'A', 'J', but I need it to allow me to enter 'Jhon', 'Maria'

CodePudding user response:

You can try this :

ALTER TABLE [CTPlatform].[Users] 
ADD CONSTRAINT CK_Users_Firstname 
       CHECK (FirstName NOT LIKE '%[^a-zA-Z]%') 
       
  • Related