我有一个名为 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
最佳答案
您可以使用正则表达式来匹配您的条件 ^[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
关于sql - 选择等于 10 位的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669808/