是否可以使用“Where”? SQL 中的子句仅显示仅包含字母 & 的字段数字?

发布于 2024-12-01 14:05:54 字数 238 浏览 0 评论 0原文

我希望能够仅选择某个字段同时包含字母和数字的字段。例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

转瞬即逝 2024-12-08 14:05:54

LIKE 会做到的。这是双重否定,

where [field2] NOT LIKE '%[^0-9a-z]%'

它表示:

  • %[^0-9a-z]% 表示 not (alphanumeric)
  • NOT LIKE '%[^0-9a- z]%' 表示 not(not(alphanumeric)) ->字母数字

编辑:

对于所有数字...“它有效”

SELECT 'it works' WHERE '1234567' NOT LIKE '%[^0-9a-z]%'

所有字母

SELECT 'it works' WHERE 'abcdefg' NOT LIKE '%[^0-9a-z]%'

包含非字母数字

SELECT 'it works' WHERE 'abc_123' NOT LIKE '%[^0-9a-z]%'

编辑2:

此解决方案适用于

仅限字母数字,字母和数字的任意组合

编辑 3:

字母后跟数字

where [field2] NOT LIKE '%[^0-9a-z]%' AND [field2] LIKE '[a-z]%[0-9]'

编辑:

最后是 2 个字母和最多 3 个数字

where
   [field2] LIKE '[a-z][a-z][0-9]'
   OR
   [field2] LIKE '[a-z][a-z][0-9][0-9]'
   OR
   [field2] LIKE '[a-z][a-z][0-9][0-9][0-9]'

LIKE will do it. This is a double negative

where [field2] NOT LIKE '%[^0-9a-z]%'

It says:

  • %[^0-9a-z]% means not (alphanumeric)
  • NOT LIKE '%[^0-9a-z]%' means not(not(alphanumeric)) -> alphanumeric

Edit:

For all numbers... "it works"

SELECT 'it works' WHERE '1234567' NOT LIKE '%[^0-9a-z]%'

All letters

SELECT 'it works' WHERE 'abcdefg' NOT LIKE '%[^0-9a-z]%'

Contains non-alphanumeric

SELECT 'it works' WHERE 'abc_123' NOT LIKE '%[^0-9a-z]%'

Edit 2:

This solution is for

only alphanumeric, any mixture of letters and numbers

Edit 3:

letters followed by numbers

where [field2] NOT LIKE '%[^0-9a-z]%' AND [field2] LIKE '[a-z]%[0-9]'

Edit:

Finally, 2 letters and upto 3 numbers

where
   [field2] LIKE '[a-z][a-z][0-9]'
   OR
   [field2] LIKE '[a-z][a-z][0-9][0-9]'
   OR
   [field2] LIKE '[a-z][a-z][0-9][0-9][0-9]'
挽手叙旧 2024-12-08 14:05:54

如果你需要它同时包含数字和字母,而不包含其他字符,我认为你必须使用 3 个 like 子句。正如 @gbn 所说,一个 NOT LIKE,然后是 2 个 LIKE 以确保表示两个字符类:

select * from (select '123' union all select 'abc' union all select 'a2') t(Field)
where Field LIKE '%[0-9]%' and Field like '%[a-z]%'
AND Field NOT LIKE '%[^0-9a-z]%'

返回一行,其中包含 'a2'


如果它应该只是字母后跟数字,我想你也许能够通过进一步的不喜欢来实现这一点,再次受到@gbn的启发:

NOT LIKE '%[0-9]%[a-z]%'

但它开始看起来像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 2 LIKEs to ensure both character classes are represented:

select * from (select '123' union all select 'abc' union all select 'a2') t(Field)
where Field LIKE '%[0-9]%' and Field like '%[a-z]%'
AND Field NOT LIKE '%[^0-9a-z]%'

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:

NOT LIKE '%[0-9]%[a-z]%'

But it is starting to look like a regex in CLR might be the preferred route.

玩套路吗 2024-12-08 14:05:54

您想要做的是基于 SQL 的正则表达式匹配。看看这个:http://msdn.microsoft.com/en-us/magazine /cc163473.aspx

引用:

“虽然 T-SQL 对于大多数数据处理来说非常强大,但它对文本分析或操作提供的支持很少。尝试使用内置字符串函数执行任何复杂的文本分析导致难以调试和维护的巨大函数和存储过程。”

并且:

“然而,SQLCLR 是一种 CLR 用户定义函数 (UDF),可让您使用 Microsoft® .NE​​T 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

信愁 2024-12-08 14:05:54

我相信 PATINDEX 会为您解决问题。下面的查询检查非 0-9 和非 az 字符,如果没有找到任何字符(即只有 # 和字母),则返回 0

Select [field1], [field2] 
from [db1].[table1] 
where patindex('%[^0-9a-z]%', [field2]) = 0

I 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)

Select [field1], [field2] 
from [db1].[table1] 
where patindex('%[^0-9a-z]%', [field2]) = 0
天涯离梦残月幽梦 2024-12-08 14:05:54
Select [field1], [field2] 

from [db1].[table1] 

where [field2] REGEXP '^[0-9a-fA-F]*

Select [field1], [field2] 

from [db1].[table1] 

where [field2] REGEXP '^[0-9a-fA-F]*

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文