shsh
shsh

Reputation: 727

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

Upvotes: 1

Views: 1174

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562260

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` 

Upvotes: 1

Related Questions