Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for one column that has numbers and letters but not one or the other

I am attempting to search a column that contains alphanumeric ids in it but want to write a query that returns records with letters and numbers but not one or the other.

i.e Acceptable: jjk44kndkfndFF

i.e Not acceptable: 223232323232   or  aajnfdskDFdd

So far I have:

where  PATINDEX('%[^a-zA-Z0-9 ]%',columnInQuestion)

This returns all alphanumeric records. Any direction appreciated

like image 807
DasBooten Avatar asked Oct 26 '25 08:10

DasBooten


1 Answers

I think you need three predicates in the WHERE clause:

WHERE (columnInQuestion NOT LIKE '%[^a-zA-Z0-9]%') AND 
      (PATINDEX('%[a-zA-Z]%', columnInQuestion) <> 0) AND
      (PATINDEX('%[0-9]%', columnInQuestion) <> 0)
  • First predicate (columnInQuestion NOT LIKE '%[^a-zA-Z0-9]%') is true if columnInQuestion contains only alphanumeric characters
  • Second predicate (PATINDEX('%[a-zA-Z]%', columnInQuestion) <> 0) is true if there is at least one alphabetic character in columnInQuestion
  • Third predicate (PATINDEX('%[0-9]%', columnInQuestion) <> 0) is true if there is at least one numeric character in columnInQuestion
like image 170
Giorgos Betsos Avatar answered Oct 28 '25 23:10

Giorgos Betsos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!