JackyBoi
JackyBoi

Reputation: 2773

Following regex is not working

I have this constraint in the SQL Server statement.

([VehNo] like '[a-zA-Z][a-zA-Z],[a-zA-Z][0-9][0-9][0-9][0-9][a-zA-Z]')

But when I try to insert data this is giving me error.

INSERT INTO [dbo].Car
VALUES ('SGD1234F','Ferrari','F30','2014-09-24','500000.00','500.00','excellent');

The error is as follows

Msg 547, Level 16, State 0, Line 2 The INSERT statement conflicted with the CHECK constraint "vehNo_ck_validity". The conflict occurred in database "t322", table "dbo.Car", column 'VehNo'. The statement has been terminated.

What is the change that I have to make?

Upvotes: 0

Views: 127

Answers (1)

arvinchhi
arvinchhi

Reputation: 477

Your regexp contains an unwanted comma. I have added spaces to highlight comma in your regexp.

[a-zA-Z][a-zA-Z] , [a-zA-Z][0-9][0-9][0-9][0-9][a-zA-Z]

For such regexp, VehNo should be SG,D1234F

Remove extra comma and your insert statement will work.

This is another version of your regexp: [a-zA-Z]{3}\d{4}[a-zA-Z]

Upvotes: 3

Related Questions