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

前端 未结 5 757
攒了一身酷
攒了一身酷 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

    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.

提交回复
热议问题