Select 行等于 10 位数

Select rows that equals to 10 digit

我有一个名为 idx 的 table 具有以下数据

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

如何 select 只有数字的行,它应该只有 10 位数字

预期输出是

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

您可以使用正则表达式来匹配您的条件 ^[0-9]{10,10}$,我已经使用 replace() 函数删除字符之间的空 space(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