Home > Back-end >  Pattern Matching using REGEXP in MySQL
Pattern Matching using REGEXP in MySQL

Time:03-08

I'm trying to make a column in mysql called group if from the second through the third position of the string matches exactly 07. For instance, the string a07ha should be categorized as 07 since this satisfies the condition. This gives me an error. I guess I'm messed up with the = '07' part. Any advice would be of great help.

SELECT
    CASE WHEN 'a07ha' REGEXP '^{2,3}' = '07' THEN '07'
    ELSE '00'
END AS group

CodePudding user response:

You described your condition:

if from the second through the third position of the string matches exactly 07.

The {2,3} pattern does not need to be used for this. All you need to check that the literal characters 07 follow the first character.

SELECT CASE WHEN 'a07ha' REGEXP '^.07' THEN '07' ELSE '00' END AS `group` 
  • Related