[英]Validate phone number using SQL
I need to check if phone number is in below format or not我需要检查电话号码是否为以下格式
+1(111)111-1111
(111)111-1111
1111111111
I am using this logic but not getting what I want我正在使用这个逻辑,但没有得到我想要的
DECLARE @Customers TABLE (PhoneNumber VARCHAR(20))
INSERT @Customers
VALUES
('+1(111)111-1111'),
('1111111111'),
('11(11)111111'),
('11abcd1111'),
(')123(45678-9-0-'),
('(111)111-1111')
SELECT PhoneNumber
FROM @Customers
WHERE PhoneNumber NOT LIKE '%[^0-9()-]%'
--AND REPLACE(REPLACE(REPLACE(PhoneNumber, '(', ''), ')', ''), '-', '') LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
AND LEN(REPLACE(REPLACE(REPLACE(REPLACE(PhoneNumber, '(', ''), ')', ''), '-', ''),'+','')) = 10 -- Alternate test
The result I am getting我得到的结果
1111111111
11(11)111111
)123(45678-9-0-
UPDATE : +1(111)111-1111 should also be in the result list l but it is not.更新:+1(111)111-1111 也应该在结果列表中 l 但它不是。 How can I add it?
我怎样才能添加它?
SELECT PhoneNumber
FROM @Customers
WHERE PhoneNumber LIKE replace('+1(111)111-1111%','1','[0-9]')
OR PhoneNumber LIKE replace('(111)111-1111%','1','[0-9]')
OR PhoneNumber LIKE replace('1111111111%','1','[0-9]')
My query :我的查询:
DECLARE @Customers TABLE (PhoneNumber VARCHAR(20))
INSERT @Customers
VALUES
('+1(111)11-1111'),
('2222222222'),
('11(11)111111'),
('11'),
(')123(45678-9-0-'),
('(111)111-1111')
SELECT PhoneNumber
FROM @Customers
WHERE
PhoneNumber LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
or PhoneNumber LIKE '[+][0-9][(][0-9][0-9][0-9][)][0-9][0-9][-][0-9][0-9][0-9][0-9]'
or PhoneNumber LIKE '[(][0-9][0-9][0-9][)][0-9][0-9][0-9][-][0-9][0-9][0-9][0-9]'
Output:输出:
PhoneNumber![]() |
---|
+1(111)11-1111 ![]() |
2222222222 ![]() |
(111)111-1111 ![]() |
Let me know if you want to replace the bracket or +/- sign.如果您想替换括号或+/-符号,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.