我如何编写查询来检索正好包含五个字母的模式。
我有这个问题要回答:
列出名称恰好由 5 个字母组成的所有客户名称。
我写的查询是这样的:
SELECT ContactName
From Customers
WHERE ContactName LIKE '[A-Z][A-Z][A-Z][A-Z][A-Z]'
谢谢
最佳答案
SELECT ContactName
FROM Customers
WHERE LEN(ContactName) = 5 AND ContactName Not LIKE '%[0-9]%' AND ContactName Not LIKE '%[^a-zA-Z0-9]%'
我还没有测试过,但它应该可以工作
我的方法是检查长度应该等于 5 并且不应该包含任何数字或特殊字符。
我接受了帮助
How to detect if a string contains at least a number?
也引用了
How to detect if a string contains special characters?
并阅读
https://www.educative.io/edpresso/what-is-not-like-operator-in-sql
关于sql - 类似运算符的基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48995195/