[FIXED] How to avoid inserting special characters in a SQL Server column?

Issue

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'


Solution

You can try this :

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


Answered By - Md. Suman Kabir
Answer Checked By - Senaida (sql.easybugfix Volunteer)

Related Posts

Post a Comment