Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 764
Dylan Avatar asked Apr 20 '11 17:04

Dylan


People also ask

How do you split a string using a separator?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do I split a string in MySQL?

In MySQL, we use SUBSTRING_INDEX() to split the string. It usually consists of three arguments i.e., string, delimiter, and position. The string value will be split based on the position.

How split comma separated values in SQL query?

Split comma-separated value string in a column. SELECT ProductId, Name, value FROM Product CROSS APPLY STRING_SPLIT(Tags, ','); Here is the result set. The order of the output may vary as the order is not guaranteed to match the order of the substrings in the input string.

How do you separate a string in SQL?

The STRING_SPLIT(string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT() function and ' ' as the second argument. FROM STRING_SPLIT( 'An example sentence.


1 Answers

SELECT SUBSTRING_INDEX(column_name, '==', 1) FROM table ; // for left  SELECT SUBSTRING_INDEX(column_name, '==', -1) FROM table; // for right 
like image 68
Shakti Singh Avatar answered Sep 22 '22 14:09

Shakti Singh