'Adding constraint to check if the value contains specific characters- SQL Server
My Check constraint conflicts when adding values, any answer to why it is wrong will be much appreciated.
Here is the question:
Ensure that the Student’s NIC number contains 9 digits (0-9) and one character which is “V” or “v”.
Here is the value I want to add to the table:
946785467v
Here is the constraint I used:
ALTER TABLE Student
ADD CONSTRAINT stdNIC CHECK(NIC LIKE '[0-9]{9}[V-v]');
Solution 1:[1]
Better to change the constraint as below.
ALTER TABLE Student ADD CONSTRAINT stdNIC CHECK(NIC LIKE '%[0-9]%[V-v]')
Example validated on db<>fiddle<>example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Srishuk Kumar Bhagwat |
