|
-
December 5th, 2006, 12:18 AM
#1
Splitting column delimited by multiple Carriage Return
I was unsuccesful finding a thread on this topic to my surprise so I am sorry if I am doubling up.
I am using sql server 2000 to extract data from a software package.
This package contains 2 columns in the table.
Postal Address and Actual Address.
I want to use the postal address, but if it is null I take the actual address.
Both address columns are varchar 255 and contain the full address details.
The data is not uniform, in other words the package allows the user to enter address information without structuring address into specific lines.
It does allow carriage return delimiters within the columns that separate the address information.
So I have columns containing any number of carriage returns.
I want to allow for no carriage returns ( meaning one line of address info is present), up to 2 carriage returns (meaning that there are 3 lines of address info).
My approach involves the following functions
len, substring, charindex, replace, isnull
Below is the code that I am using to create 2 lines of address information.
I believe it is close to achieveing the result but it has one problem, when there are no carriage returns, it defaults to placing the address into line 2 rather than line 1.
The replace function was my last attempt to force this into the first line. It fails because the replace sets up a varchar for arguement 3 in the substring which is an invalid format for the substring.
If anyone has a better approach I am all ears.
Thanks to anyone willing to put the time into this code, it certainly doesn't look easy to read.
Code:
select clientid,
isnull(postaladdress,actualaddress) as fulladdress,
substring(isnull(postaladdress,actualaddress),1,
replace(charindex(char(13),isnull(postaladdress,actualaddress)),0,len(isnull(postaladdress,actualaddress)))) as addressline1
--(len(isnull(postaladdress,actualaddress)) -
--(len(isnull(postaladdress,actualaddress)) - charindex(char(13),isnull(postaladdress,actualaddress))))) as addr1
--substring(isnull(postaladdress,actualaddress),1+charindex(char(13),isnull(postaladdress,actualaddress)),100) as addressline2
from clients
Last edited by T2T2; December 5th, 2006 at 12:48 AM.
TT
-
December 6th, 2006, 01:13 AM
#2
Re: Spliting column delimited by multiple Carriage Return
For anyone who is interested, I have worked this problem and come up with the following solution.
Within my select statement I use Case statements to set up the 3 address lines.
Code:
addr1 =
case
when (charindex(char(13),isnull(postaladdress,actualaddress))) = 0 then isnull(postaladdress,actualaddress)
else substring(isnull(postaladdress,actualaddress),1,charindex(char(13),isnull(postaladdress,actualaddress)) )
end,
addr2 =
case
when (charindex(char(13),isnull(postaladdress,actualaddress))) = 0 then ' '
when charindex(char(13),
substring(isnull(postaladdress,actualaddress),1+charindex(char(13),
isnull(postaladdress,actualaddress)),200)) = 0
then substring(isnull(postaladdress,actualaddress),1+charindex(char(13),isnull(postaladdress,actualaddress)),200)
else substring(isnull(postaladdress,actualaddress),1+charindex(char(13),isnull(postaladdress,actualaddress)),
charindex(char(13),
substring(isnull(postaladdress,actualaddress),1+charindex(char(13),
isnull(postaladdress,actualaddress)),200)))
end,
addr3 =
case
when (charindex(char(13),isnull(postaladdress,actualaddress))) = 0 then ' '
when charindex(char(13),
substring(isnull(postaladdress,actualaddress),1+charindex(char(13),
isnull(postaladdress,actualaddress)),200)) = 0 then ' '
else substring(isnull(postaladdress,actualaddress),charindex(char(13),isnull(postaladdress,actualaddress)) +
charindex(char(13),
substring(isnull(postaladdress,actualaddress),1+charindex(char(13),isnull(postaladdress,actualaddress)),200)),
100)
end,
TT
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|