MySQL : left part of a string split by a separator string?

Dylan picture Dylan · Apr 20, 2011 · Viewed 42.8k times · Source

I need a MySQL function to get the left part of a string with variable length, before the separator.

For example, with separator string '==' :

abcdef==12345     should return abcdef
abcdefgh==12      should return abcdefgh

Also the same thing, but for the right part...

Answer

Shakti Singh picture Shakti Singh · Apr 20, 2011
SELECT SUBSTRING_INDEX(column_name, '==', 1) FROM table ; // for left

SELECT SUBSTRING_INDEX(column_name, '==', -1) FROM table; // for right