0

I have a string in a column in mysql as follows:

761696495 - 689552180|723146573 - 684706476|295453556 - 656883782

Is there a way that I can check against that string, for example lets say I want to look either for 723146573 or 684706476, which is the middle item in the delimited piped string, I want to be able to return the whole portion of that string if a match is made. So for example if I wanted to check against 684706476, it would return:

723146573 - 684706476

And as the reverse, if I want to match 723146573, it would return the full match in that position too

723146573 - 684706476

Also just to not make any confusion, essentially I want to be able to match against this whole string for any position of it and if a hit is made it returns the first part of the - and last in that position it is found, hopefully that makes sense.

5
  • 1
    Are you stuck with this format? It would be much better to normalize your table. Commented Apr 18, 2024 at 1:03
  • At the moment yes :( So this is why I'm trying to extract from it ideally. I've tried using various versions of substring etc.. but I can't seem to figure out with this method how to do it. Commented Apr 18, 2024 at 1:04
  • Use a regular expression to match the number, e.g. [:<:]723146573[:>:]. Then write a stored function that takes the whole column and the search string, loops over the pipe-delimited substrings, and returns the substring that contains the seach string. Commented Apr 18, 2024 at 1:07
  • And before you ask, no, I won't write the function for you. Commented Apr 18, 2024 at 1:08
  • See stackoverflow.com/questions/7737970/… for how to loop over a delimited list. Commented Apr 18, 2024 at 1:09

1 Answer 1

0

Try it! If u use only MySQL, maybe following query is helpful. SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '|', position), '|', -1) AS match FROM table_name WHERE LOCATE('684706476', column_name) > 0;

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.