ImFluffy
ImFluffy

Reputation: 1

SQL Server telephone regex check

Currently for school I need to make a Check constraint for phonenumbers this needs to be done in SQL Server.

I need to check this format:

(xxx) xxx-xxxx

I also need to include the brackets and whitespaces

So far I tried:

SELECT PhoneNumber
FROM Sales.Customers
WHERE PhoneNumber LIKE '[(][0-9]{3}[)]\s[0-9]{3}[-][0-9]{4}'

But this did not work unfortunately. What am I doing wrong here? Please help.

Upvotes: 0

Views: 1596

Answers (1)

Thom A
Thom A

Reputation: 95621

SQL Server does not support Regex natively. It does have pattern matching, however, the functionality is no where near that of Regex.

For what you have you would need to do:

SELECT PhoneNumber
FROM Sales.Customers
WHERE PhoneNumber NOT LIKE '([0-9][0-9][0-9]) [0-9][0-9][0-9][-][0-9][0-9][0-9][0-9]';

Edit: OP has now edited to LIKE (was NOT LIKE). Not sure which they want, so I have left as NOT LIKE, as per their original version.

Upvotes: 2

Related Questions