I have a street address string pulling into a table, that is separated by char(10). I am trying to separate the string so that I can map that field into another system, that has Addressline1, AddressLine2, AddressLine3. I've been working with the following:
,case when CHARINDEX(char(10),[BillingStreet])= 0 then BillingStreet else substring(BillingStreet,1,charindex(char(10),BillingStreet)-1) end AddressLine1
,case when CHARINDEX(char(10),[BillingStreet])= 0 then '' else substring(BillingStreet,charindex(char(10),BillingStreet)+1,charindex(char(10),BillingStreet)-1) end AddressLine2
,case when CHARINDEX(char(10),[BillingStreet])= 0 then '' else reverse(substring(reverse(BillingStreet),0,charindex(char(10),reverse(BillingStreet)))) end AddressLine3
My issue is that if an address does not have a 3rd char(1), then my columns AddressLine2 & AddressLine3 will be the same. Should I use a different case statement on AddressLine3 or be using a different substring statement?
Sample street address: 1234 Main Street Unit 123 BOX 123
Desired output:
Addressline1 = 1234 Main Street
Addressline2 = Unit 123
Addressline3 = BOX 123