【问题标题】:Clean phone numbers which has special characters and alphabets清理具有特殊字符和字母的电话号码
【发布时间】:2016-06-23 07:53:09
【问题描述】:

我的电话号码字段中有垃圾,我想清理它们。我想知道如何查询以检查电话号码字段中是否有任何特殊字符、字母。有人可以帮忙吗? 我试过这个查询但没有用。我需要PostgreSQL中的代码

select phone from table where phone like '[^[:alpha:]]' and phone <>''
-- and phone not like '[^0-9]'
order by phone

表格中的输入值如下:

Phone
-----
(443)-554-6677
111-111-1111
345-rty-34fr
4345434444 ext

输出(应该看起来像这个有效的电话号码)

(443)-554-6677
111-111-1111

感谢您的帮助。

谢谢你, 斯瓦蒂。

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    我们可以使用POSIX regular expressions 来获得所需的输出:

    select phone from t1 where phone<>'' and phone!~'[[:alpha:]]';
    

    您似乎正在尝试将正则表达式语法与 like 运算符一起使用,但这是不正确的。 like operator 非常有限;它基本上只提供%前缀/后缀通配符和_单字符通配符。


    如果你想更严格,我们可以这样做:

    select phone from t1 where phone~'^(\([0-9]{3}\)|[0-9]{3})-[0-9]{3}-[0-9]{4}$';
    


    测试夹具

    drop table if exists t1;
    create table t1 (phone text);
    insert into t1 (phone) values ('(443)-554-6677'), ('111-111-1111'), ('345-rty-34fr'), ('4345434444 ext');
    

    【讨论】:

    • 你太棒了。非常感谢您的解释和回答我的问题。它对我有用。
    • 嗨,如果我想过滤那些有垃圾的坏电话号码,就像所有 10 位数字都有相同的号码一样,我该如何过滤它们。例如数字模式:像 111111 数字模式:像 000000数字模式:like 999999 Phone -------- 333-333-3333 5555555555 (666)666-666 00011111111 我想知道这种甲酸盐的数量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 2019-09-10
    • 2016-03-21
    • 1970-01-01
    • 2018-05-08
    • 2010-11-16
    相关资源
    最近更新 更多