0

I have a table with 2 columns, one for id and one for country.

I've found some rows in the table where it looks like the below:

ID Country
1 US and Canada
2 France and UK

How can I write a query so that I find the rows and split the column so I get the following result.

ID Country
1 US
1 Canada
2 France
2 UK
6
  • Does this answer your question? Substring after a space Commented Jun 9, 2021 at 6:59
  • Do you have more than one andin a row? Commented Jun 9, 2021 at 7:02
  • Refer : stackoverflow.com/questions/6233037/… Commented Jun 9, 2021 at 7:06
  • @sddk I just have one Commented Jun 9, 2021 at 7:12
  • @GehanFernando Not applicable, check DBMS tag. Commented Jun 9, 2021 at 7:21

2 Answers 2

3

Do you have more than one and in a row? – sddk

I just have one – jean10

SELECT id, SUBSTRING_INDEX(Country, ' and ', 1) Country
FROM tablename
UNION 
SELECT id, SUBSTRING_INDEX(Country, ' and ', -1)
FROM tablename
ORDER BY 1
Sign up to request clarification or add additional context in comments.

Comments

0
    SELECT id, SUBSTRING_INDEX(name, ' and ', 1) as country
        FROM 
           (      SELECT 1 AS id, 'US and CN' AS name 
            UNION SELECT 2 AS id, 'JP and KR' AS name
           ) as string_test_tbl
        UNION 
    SELECT id, SUBSTRING_INDEX(name, ' and ', -1) as country
        FROM 
           (      SELECT 1 AS id, 'US and CN' AS name 
            UNION SELECT 2 AS id, 'JP and KR' AS name
           ) as string_test_tbl
        ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.