1

I want to check whether there is a known pattern with variable numbers.

This column 'shortcut' has values like this

|shortcuts|
-----------
|ab1
|ab2
|ab23
|abc123

The only thing I've got for my SQL-statement is the alphabetical pattern e.g. 'ab'

So I started with

SELECT * FROM mytable WHERE shortcut LIKE 'ab%'

I only need ab1, ab2 and ab23 and NOT abc12.

Is there a way to modify my statement? There is at least one number, numbers always follow the known pattern and the pattern is the only known value.

1
  • Sorry, does it mean you want to also return records like ab12 some text here? Commented Sep 9, 2020 at 9:25

2 Answers 2

1

You can use regular expressions:

where shortcut regexp '^ab[0-9]+'

This says that shortcut starts with "ab" and is followed by at least one digit.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this statement did exactly what I needed.
1

You can use

SELECT * FROM mytable WHERE shortcut REGEXP '^ab[0-9]+$'

The ^ab[0-9]+$ regex (see its online demo) matches:

  • ^ - start of string
  • ab - an ab string (case insensitively, use BINARY after REGEXP to make it case sensitive)
  • [0-9]+ - one or more digits
  • $ - end of string.

See this regex graph:

enter image description here

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.