Dylan
Dylan

Reputation: 9373

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

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

Upvotes: 36

Views: 48890

Answers (3)

Brandon
Brandon

Reputation: 910

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

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86366

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

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

Upvotes: 84

Nicola Cossu
Nicola Cossu

Reputation: 56357

select substring_index('abcdef==12345','==',1)

for the right part use -1 instead of 1.

Upvotes: 7

Related Questions