Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split address with multiple delimeter to multiple column in SQL

1808 FOX CHASE DR, GOODLETTSVILLE, TN i am trying to split it in 3 column based on , comma.

I have used select substring_index(OwnerAddress,',',1),substring_index(OwnerAddress,',',-1) from housingdata; it will give me only 2 values however i am lookking for 3 values in 3 different column.

i am using Mysql Workbench. can anyone guide on how to split string with multiple delemter in sql.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

mysql> set @a = '1808  FOX CHASE DR, GOODLETTSVILLE, TN';

mysql> select trim(substring_index(@a, ',', 1)) as address,
  trim(substring_index(substring_index(@a, ',', 2), ',', -1)) as city,
  trim(substring_index(@a, ',', -1)) as state;
+--------------------+----------------+-------+
| address            | city           | state |
+--------------------+----------------+-------+
| 1808  FOX CHASE DR | GOODLETTSVILLE | TN    |
+--------------------+----------------+-------+

On the other hand, if you want these three data elements to be separate, you should store them in three separate columns. This sort of query is bound to be trouble if someone has an address that contains a comma.

See https://linguaholic.com/linguablog/commas-in-adresses/

4344 Lynn Ave., Building F, Apt. 4B, Merrill, WI 54452

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading