1

I have a column that contains values like 5A898, 89KAS, 89ASD.

I'm trying to write a query that will only return rows where the third digit of the column is 'A'. For instance '89ASD' would be returned but '89KAS' would not. I'm not sure what the right way to go about this is. Regular expressions?

So...

SELECT column
FROM table
WHERE column = ?

4 Answers 4

2
WHERE column LIKE '__A%'
                   ^^-- 2 underscores

should do the trick. two "whatever" characters, followed by an A, then followed by anything in any amount.

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

4 Comments

How efficient would this be versus SUBSTRING(column, 3, 1) = 'A' ?
about the same. since you're using wildcards and derived values, neither will allow indexes to be used.
Agreed, but surely pattern matching carries some significant overhead versus directly accessing the third character of a string?
depends on how smart the query optimizer is. __ is fairly easy to handle. doing %A% would be a bit heavier.
0

Maybe you can use MySQL's SUBSTRING

SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A'

Comments

0
where right(left(col,3),1) = 'A'

That can help... MarcB's answer is cleaner

Comments

0

You can do this with a regex, but I think it might be easier just coping with a string operation here:

SELECT column
FROM   table
WHERE  SUBSTRING(column,3,1) = 'A';

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.