1

I have a full length item code that I am looking for, '34FDE353UE2' and a table full of a shortened version of said item codes, like this

itemCode
============
34FDE
35DCF
34FPE
....

How can I look through my rows to see if any of them match the '34FDE353UE2'? In my example, I would hope to get back the first row since 34FDE is a substring of '34FDE353UE2'. How can I write a query to do this?

Using MySQL 5.6.

bjk116
  • 531
  • 3
  • 16

2 Answers2

5

With like:

select * from tablename
where '34FDE353UE2' like concat(itemCode, '%')

this will return rows where itemCode's value is the starting chars of '34FDE353UE2'.
If you want rows where itemcode is a substring of '34FDE353UE2' then:

select * from tablename
where '34FDE353UE2' like concat('%', itemCode, '%')
forpas
  • 160,666
  • 10
  • 38
  • 76
  • That was the easiest way. Thank you. I'd give you the solution but turns out my question was a duplicate. – bjk116 Mar 07 '19 at 14:58
0

this will work:

select * from tablename where  REGEXP_SUBSTR(itemCode,'34FDE353UE2')
Nikhil S
  • 3,786
  • 4
  • 18
  • 32