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

前端 未结 5 722
攒了一身酷
攒了一身酷 2021-01-14 07:56

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].[tabl         


        
相关标签:
5条回答
  • 2021-01-14 08:00

    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

    0 讨论(0)
  • 2021-01-14 08:09

    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]'
    
    0 讨论(0)
  • 2021-01-14 08:09

    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.

    0 讨论(0)
  • 2021-01-14 08:12

    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
    
    0 讨论(0)
  • 2021-01-14 08:17
    Select [field1], [field2] 
    
    from [db1].[table1] 
    
    where [field2] REGEXP '^[0-9a-fA-F]*$'
    
    0 讨论(0)
提交回复
热议问题