是否可以使用“Where”? SQL 中的子句仅显示仅包含字母 & 的字段数字?
我希望能够仅选择某个字段同时包含字母和数字的字段。例如:
Select [field1], [field2]
from [db1].[table1]
where [field2] = *LETTERS AND NUMBERS*
我使用 SQL Server 2005,也很抱歉,我不能百分百确定该字段的数据类型,因为它位于链接服务器上并且目前无法访问。希望你能帮忙
:)
I want to be able to select only the field where a certain field contains both letters and numbers. for example:
Select [field1], [field2]
from [db1].[table1]
where [field2] = *LETTERS AND NUMBERS*
Im using SQL Server 2005, also im sorry bu im not a hundred percent sure about the data type of the field because it is on a linked server and un-accessibleat the minute. Hope you can help
:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
LIKE 会做到的。这是双重否定,
它表示:
%[^0-9a-z]%
表示not (alphanumeric)
NOT LIKE '%[^0-9a- z]%'
表示not(not(alphanumeric))
->字母数字编辑:
对于所有数字...“它有效”
所有字母
包含非字母数字
编辑2:
此解决方案适用于
编辑 3:
字母后跟数字编辑:
最后是 2 个字母和最多 3 个数字
LIKE will do it. This is a double negative
It says:
%[^0-9a-z]%
meansnot (alphanumeric)
NOT LIKE '%[^0-9a-z]%'
meansnot(not(alphanumeric))
-> alphanumericEdit:
For all numbers... "it works"
All letters
Contains non-alphanumeric
Edit 2:
This solution is for
Edit 3:
letters followed by numbersEdit:
Finally, 2 letters and upto 3 numbers
如果你需要它同时包含数字和字母,而不包含其他字符,我认为你必须使用 3 个 like 子句。正如 @gbn 所说,一个
NOT LIKE
,然后是 2 个LIKE
以确保表示两个字符类:返回一行,其中包含
'a2'
。如果它应该只是字母后跟数字,我想你也许能够通过进一步的不喜欢来实现这一点,再次受到@gbn的启发:
但它开始看起来像CLR中的正则表达式可能是首选路线。
If you need it to contain both numerics and letters, and no other characters, I think you have to use 3 like clauses. One
NOT LIKE
, as @gbn said, then 2LIKE
s to ensure both character classes are represented:returns one row, with
'a2'
.If it should only be letters followed by numbers, I'm thinking you might be able to achieve this with a further not like, again inspired by @gbn:
But it is starting to look like a regex in CLR might be the preferred route.
您想要做的是基于 SQL 的正则表达式匹配。看看这个:http://msdn.microsoft.com/en-us/magazine /cc163473.aspx
引用:
“虽然 T-SQL 对于大多数数据处理来说非常强大,但它对文本分析或操作提供的支持很少。尝试使用内置字符串函数执行任何复杂的文本分析导致难以调试和维护的巨大函数和存储过程。”
并且:
“然而,SQLCLR 是一种 CLR 用户定义函数 (UDF),可让您使用 Microsoft® .NET Framework 创建高效且不易出错的函数集。”
然后你会得到代码示例。微软不是很厉害吗? :D
What you would want to do is SQL-based regexp matching. Check this out: http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
Quotes:
"Although T-SQL is extremely powerful for most data processing, it provides little support for text analysis or manipulation. Attempting to perform any sophisticated text analysis using the built-in string functions results in massively large functions and stored procedures that are difficult to debug and maintain."
And:
"However there's SQLCLR, a CLR user-defined function (UDF) that lets you create an efficient and less error-prone set of functions using the Microsoft® .NET Framework."
Then you get code examples. Isn't Microsoft great? :D
我相信
PATINDEX
会为您解决问题。下面的查询检查非 0-9 和非 az 字符,如果没有找到任何字符(即只有 # 和字母),则返回 0I believe
PATINDEX
will do the trick for you. The query below checks for non 0-9 and non a-z characters, returning 0 if it doesn't find any (i.e., only #s and letters)