【问题标题】:Select rows that equals to 10 digit选择等于 10 位的行
【发布时间】:2015-11-12 10:53:54
【问题描述】:

我有一个名为 idx 的表格,其中包含以下数据

id dex          
-- ------------ 
1  Major 12354  
2  CITY 541 DER 
3  9987741221   
4  7845741      
5  789456 2121  

如何选择只有数字的行,它应该只有 10 个数字

预期输出是

id dex         
-- ----------- 
3  9987741221  
5  789456 2121 

【问题讨论】:

    标签: sql regex postgresql string-function


    【解决方案1】:

    您可以使用正则表达式来匹配您的条件^[0-9]{10,10}$,我已经使用replace() 函数来删除字符之间的空格(dex in id 5

    select * 
    from idx 
    where replace(dex,' ','')~'^[0-9]{10,10}$' -- or '^[0-9]{10,}$'
    

    ^[0-9]{10,10}$

    [0-9] - 获取数字

    {10,10}$ - 在你的情况下,所选字符串的最大值和最小值都是10


    OR

    使用char_length()

    select *
    from idx 
    where  dex~'^[0-9]' 
           and char_length(replace(dex,' ','')) = 10 
    

    【讨论】:

    • {10,10} 可以替换为{10}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    相关资源
    最近更新 更多