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

前端 未结 5 779
攒了一身酷
攒了一身酷 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: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]'
    

提交回复
热议问题