2

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
6
  • I would suggest that it would be better to make a group field that contains OLNY the group code and nothing else Commented Mar 7, 2022 at 18:09
  • REGEXP doesn't return the matching part, it just returns TRUE or FALSE. Commented Mar 7, 2022 at 18:11
  • 1
    Also, your regexp doesn't match characters 2-3. It returns the last 2-3 characters because the $ matches the end of the string. Commented Mar 7, 2022 at 18:13
  • 2
    Why not just use SUBSTR(column, 2, 2)? Commented Mar 7, 2022 at 18:14
  • You can't use {2,3} without a pattern before it. It means 2-3 repetitions of the preceding pattern. Commented Mar 7, 2022 at 18:19

1 Answer 1

1

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` 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.