0

I have ids and corresponding strings like this:

id    place
234   USA_NY_Buffalo

I want to split up the place string into three strings each with its own column:

id    Country  State  City
234   USA      NY     Buffalo

When I try splitting the string like this:

SELECT id, SPLIT(place,"_") FROM mytable

I get

id    place
234   USA
234   NY
234   Buffalo

Is it possible to split the string by the first "_", then split the second string again by " _ " ?

1
  • Which RDBMS are you using? Commented Apr 7, 2017 at 3:54

1 Answer 1

2

If the string would always have Country,State and City only then you could try something like.

select SUBSTRING_INDEX('USA_NY_Buffalo', '_', 1) AS country,
SUBSTRING_INDEX(SUBSTRING_INDEX('USA_NY_Buffalo', '_', 2), '_', -1) AS State,
SUBSTRING_INDEX(SUBSTRING_INDEX('USA_NY_Buffalo', '_', 3), '_', -1) AS City;
Sign up to request clarification or add additional context in comments.

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.