36

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...

Dylan
  • 9,129
  • 20
  • 96
  • 153

3 Answers3

83
SELECT SUBSTRING_INDEX(column_name, '==', 1) FROM table ; // for left

SELECT SUBSTRING_INDEX(column_name, '==', -1) FROM table; // for right
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
7
select substring_index('abcdef==12345','==',1)

for the right part use -1 instead of 1.

Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98
1

I would look into the substring function in SQL which is SUBSTR, but it is more for set positions in the string, not so much for variable lengths.

http://www.1keydata.com/sql/sql-substring.html

Brandon
  • 890
  • 2
  • 13
  • 32