Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pattern Matching using REGEXP in MySQL

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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` 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading