I create a table by query below
CREATE TABLE testing(
test_field VARCHAR(20) CHECK(test_field like '^[a-zA-z0-9]{6,20}$')
);
the regular expression is working in Javascript and the table was successfully created, but when I trying to execute the insert query below, the error occurred.
INSERT INTO testing VALUES('abcde12345');
The error message is listing below
The INSERT statement conflicted with the CHECK constraint “CK_testing_test_field__173876EA”. The conflict occurred in database “TEST”, table “dbo.testing”, column ‘test_field’.
Is there any difference between using Regular Expression in Javascript and SQL Server?
Any pro can help???
LIKEdoes not use regular expressions.You can add regular expression support to SQL Server by creating your own functions. One method via .NET is detailed in this article.
In your case you can also try the following, but it’s not pretty: