mossmis
asked on
SQL Query parsing one field into many
We have a sql server database that stores a multiline customer address in a single field. I found out that a carriage return is what separates each line.
Here is an example of data
ADDRESS
*************
123 Main StApt 1Some City, MyState 12345
*************
The address shows up in our erp as
************************** ********** *****
123 Main St
Apt 1
Some City, MyState 12345
************************** ********** *******
What I want to extract are the two address lines in a SQL query:
123 Main St as address1
Apt 1 as address2
Any suggestions? Thanks!
Here is an example of data
ADDRESS
*************
123 Main StApt 1Some City, MyState 12345
*************
The address shows up in our erp as
**************************
123 Main St
Apt 1
Some City, MyState 12345
**************************
What I want to extract are the two address lines in a SQL query:
123 Main St as address1
Apt 1 as address2
Any suggestions? Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried cy hung's code and I wasn't getting results. After some research, I gave some bad information. It wasn't a carriage return that was stored rather a line feed char(10). I replaced that in the code and got better results.
select
SUBSTRING(ADDRESS,1,CHARIN DEX(CHAR(1 0),ADDRESS )-1) as address1,
SUBSTRING (ADDRESS,CHARINDEX(CHAR(10 ),ADDRESS) +1,LEN(ADD RESS)-CHAR INDEX(CHAR (10),ADDRE SS)+1) as address2
from dbo.address
Thanks for the guidance.
select
SUBSTRING(ADDRESS,1,CHARIN
SUBSTRING (ADDRESS,CHARINDEX(CHAR(10
from dbo.address
Thanks for the guidance.
Open in new window
Could also be cariage return/lineedOpen in new window