SQL查询仅返回11位数字值
实际数据:
|31345678921|
|56789056789|
|56780345678|
|4567 |
|3456 |
|00678596 |
|03456788453|
|ASA 2344 |
|34565 |
|BBq23 |
|DNF LIMIT |
所需资料:
|31345678921|
|56789056789|
|56780345678|
|03456788453|
回答
一种方法是:
where actual not like '%[^0-9]%' and
len(actual) = 11
也就是说,该值没有非数字,长度为 11 位。
另一种方法是:
where try_convert(numeric(11,0), actual) between 10000000000 and 99999999999
也就是说,当转换为数字时,范围表明它是 11 位数字。
第三种方法是:
where actual like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
SQL Server 支持带有 的字符类like,因此您可以像[0-9]11 次一样。