-3

I have a column in my database name location which have city name and country name in this format New york,America. I want to run a select query with the explode function like which we use in php to separate values like this so that i can get comma , separated values

select explode(",",location) from address;

Also with the alias of city column holding New York and alias of country holding value of America. So that i can use them in my store procedure and insert this values in references table in the columns city and country

usama
  • 797
  • 1
  • 11
  • 44

5 Answers5

4

You can not really "explode" or split on all comma's, but you can split a string on any comma using SUBSTRING_INDEX.

SELECT SUBSTRING_INDEX('New york,America', ',', 1);
-> New york
Sjoerd
  • 71,634
  • 16
  • 123
  • 171
  • I want to split Colum values not a string @Sjoerd – usama Jun 03 '15 at 11:39
  • 1
    @usama, what difference? it's just sql function. – vp_arth Jun 03 '15 at 11:50
  • @vp_arth the difference is i want to select city,country from location colum and store this into the other table colums city and country using store procedure – usama Jun 03 '15 at 11:52
  • no any difference in your example) just use `substring_index` function 2 times, for city and for country. – vp_arth Jun 03 '15 at 11:54
  • @Sjoerd with i can get the New york but witj index 2 SUBSTRING_INDEX('New york,America', ',', 2) it return both New york,America – usama Jun 03 '15 at 12:06
  • 2
    try `-1` index instead of `2`. It will mean: `all, after last comma` – vp_arth Jun 03 '15 at 12:08
1

Use Group concat

   SELECT GROUP_CONCAT( location )
    FROM `address`
Suraj
  • 365
  • 2
  • 14
1
CREATE FUNCTION strSplit(x varchar(255), delim varchar(12), pos int)  
returns varchar(255)
return replace(substring(
  substring_index(x, delim, pos+1), 
  length(substring_index(x, delim, pos)) + 1
), delim, '');


select strSplit("aaa,b,cc,d", ',', 1) as second;
> b
vp_arth
  • 13,847
  • 4
  • 37
  • 62
0

You can also select the row and store it as a string, and use the explode function to separate the values if you are intending to use the function itself.

Everett
  • 35
  • 4
0

Assuming that exploding will be to create N number of rows, then you can do it like this.

SET @completeString = "Hola,New york,America,!,";
SET @divider = ",";

WITH RECURSIVE strings(m) AS (
    SELECT
        @completeString
    UNION ALL
    SELECT
        SUBSTRING(m, INSTR(m, @divider)+ 1)
    FROM
        strings
    WHERE
        INSTR(m, @divider)!= 0
)
SELECT
    SUBSTRING_INDEX(m, @divider, 1) exploted_strings
FROM
    strings;

That'll give you five rows, including the empty string

+----------------+
|exploted_strings|
+----------------+
| Hola           |
| New york       |
| America        |
| !              |
|                |
+----------------+

Or have it in a Procedure.

CREATE PROCEDURE explode(IN completeString TEXT, IN divider TEXT)
BEGIN
    WITH RECURSIVE strings(m) AS (
        SELECT 
            completeString
        UNION ALL
        SELECT 
            SUBSTRING(m, INSTR(m, divider)+ 1)
        FROM 
            strings 
        WHERE 
            INSTR(m, divider)!= 0
)
SELECT
    SUBSTRING_INDEX(m, divider, 1)
    FROM strings;
END

And call it like

CALL explode ("Hola,New york,America,!,",",");