IS it possible to use the 'Where' clause in SQL to only show a field containing only letters & Numbers?

爱⌒轻易说出口 提交于 2019-12-30 10:29:17

问题


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

:)


回答1:


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]'



回答2:


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




回答3:


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.




回答4:


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



回答5:


Select [field1], [field2] 

from [db1].[table1] 

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


来源:https://stackoverflow.com/questions/7204779/is-it-possible-to-use-the-where-clause-in-sql-to-only-show-a-field-containing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!