Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6994499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:54:02+00:00 2026-05-27T19:54:02+00:00

Create MySQL Split String Function SPLIT_STR fedecarg.com/…/mysql-split-string-function/ CREATE FUNCTION SPLIT_STR( x VARCHAR(255), delim VARCHAR(12),

  • 0

Create MySQL Split String Function SPLIT_STR fedecarg.com/…/mysql-split-string-function/

CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');

Run SQL:

SELECT t.en AS `type`, SPLIT_STR(l.en, ',', 1) AS city,
SPLIT_STR(l.en, ',', 2) AS country
FROM table1
JOIN table2
USING ( id )
LEFT JOIN table3 AS t ON table1.type = t.id
/* the next line has failure with SPLIT_STR */
LEFT JOIN table3 AS l ON table1.location = l.id
WHERE language.lang =  'en'
AND language.url =  'europe-countries'
LIMIT 1;

table1

id               | type            | location
-----------------+-----------------+-----------------
6BC45C02         | place           | london,england

table2

id               | url
-----------------+-----------------
6BC45C02         | europe-countries

table3

id               | en
-----------------+-----------------
london           | London
england          | England

Failed result:

type             | city            | country
-----------------+-----------------+----------------
place            | NULL            | NULL

Expected result would be to return city and country:

type             | city            | country
-----------------+-----------------+-----------------
place            | London          | England

On checking if SPLIT_STR is working with simple SQL:

SELECT SPLIT_STR(location, ',', 1) AS city, SPLIT_STR(location, ',', 2) AS contry
FROM table1
WHERE id =  '6BC45C02'
LIMIT 1;

it returns fine result:

city             | contry
-----------------+-----------------
london           | england
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:54:02+00:00Added an answer on May 27, 2026 at 7:54 pm

    Maybe this… but performance will be terrible.

    SELECT T1.type,
      SPLIT_STR(t.en, ',', 1) AS city,
      SPLIT_STR(l.en, ',', 2) AS country
    FROM table1 t1
    INNER JOIN table2 t2 
      ON T1.ID = T2.ID
    LEFT JOIN table3 t 
      ON t.id = SPLIT_STR(t1.location, ',', 1)
    LEFT JOIN table3 l 
      ON l.id = SPLIT_STR(t1.location, ',', 2)
    WHERE t2.url = 'europe-countries'
    LIMIT 1;
    

    This would be better as it doesn’t require calling the function 4 times: (used coalesce to determine if function isn’t working as expected will return proper case, then lower case, then function broke if it’s not working as expected)

    SELECT InTable.Type, 
       coalesce(t.en, inTable.City, 'FunctionBroke') as city, 
       coalesce(l.en, intable.country, 'FunctionBroke2') as country
    FROM 
        (SELECT t1.type,
          SPLIT_STR(T1.Location, ',', 1) AS City,
          SPLIT_STR(T1.Location, ',', 2) AS Country
        FROM table1 T1
        INNER JOIN table2 T2 
          ON T1.ID = T2.ID 
            AND t2.url='europe-countries'
        ) InTable
    LEFT JOIN table3 t 
      ON InTable.City = t.id
    LEFT join table3 l 
      ON InTable.Country = l.id
    LIMIT 1;
    

    Still better exists: is table 3’s only purpose to Proper case the city/country name; and is the only purpose of the UDF (user defined function) to split the values?

    • EDIT 1 Fixed first should have been t.en instead of l.en on first left
      join.
    • EDIT 2 Fixed 2nd should have been t1 (that’s copy and paste for
      you)
    • EDIT 3 Relooked at everything in both responses copied several of the errors in the OP
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a members table in MySQL CREATE TABLE `members` ( `id` int(10) unsigned
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
Is there a way to create mysql triggers using Activerecord migrations ? Has anybody
Is it possible to create a MySQL select statement that uses an expression as
I want to implement the following constraints in mysql: create table TypeMapping( ... constraint
I have a basic hosting package that gives me access to create a MySQL
Can we create a new MySQL database( not a table, I want a new
I'm using mysql to create an hotel booking system, but i am struggling a
I'm trying to create a simple database table using the PHP MySQL query Create
following is the that I create dump from mysql database. mysqldump -u root tempbkk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.