Skip to content

Instantly share code, notes, and snippets.

@bondarewicz
Last active January 4, 2016 08:29
Show Gist options
  • Save bondarewicz/8595674 to your computer and use it in GitHub Desktop.
Save bondarewicz/8595674 to your computer and use it in GitHub Desktop.
SQL: Explode address text column into separate columns
select
length(replace(address_str, '\r\n', '\r\n ')) - length(address_str) as AddressLineCount,
substring_index(substring_index(address_str,'\r\n',1),'\r\n',-1) as AddressLine1,
substring_index(substring_index(address_str,'\r\n',2),'\r\n',-1) as AddressLine2,
substring_index(substring_index(address_str,'\r\n',3),'\r\n',-1) as AddressLine3
from (
select replace(concat(address,'\r\n'),'\r\n\r\n','\r\n') as address_str
from table_location where location_id = 1
) normalized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment