Check if a string has a combination of a substring and numbers in sql

Viewed 2733

how do I write a SQL where statement that checks if a string contains some substring and a number. For example:

string: macsea01

where string like 'macsea' plus a number

2 Answers

Regex is the most obvious solution to this question. Without more detail about the specific format of the string, I can suggest the following, which will match a sequence of a letter in the alphabet followed immediately by a digit:

where column_name like '%[a-zA-Z][0-9]%'

If you're literally looking for macsea at the beginning of the string followed by a digit, it would be:

where column_name like 'macsea[0-9]%'

Regex seem to bee a little slippery here, depending on your needs you can for instance divide the string into several parts, first the text part, and take the rest of the string, try to convert it into a number.

Somthing like this (but I think this perticular code is broken

where substring(column_name, 1, 6) = 'macsea' and cast(substring(column_name, 7, 1000) as int) > 0
Related