Solved

SQL tidy up address data

Posted on 2014-03-14
4
325 Views
Last Modified: 2014-03-14
Hi All

I have an address table from which I need to print address labels. ie

Address1, Address2, Address3, PostalCode

Some of these fields are empty, so I want the next available field to be in the position of the missing data so when printed the address is on consecutive lines with no spaces.

IE
Address1
address3
Postalcode

Note address2 is missing, but there is no place holder for where it should be.

Any idea on how I can achieve this?
0
Comment
Question by:Andy Green
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 39928589
It depends what sort of application you are printing from as to what link break characters you need but something like the following should work where you can set the line break to be either a regular "\n\r" or html (I only mention HTML because thats how I normally do it).

declare @LineBreak varchar(4)

set @LineBreak = '<br>'

select
  Address1
  + case when isnull(Address2,'') <> '' then @LineBreak + Address2 else '' end
  + case when isnull(Address3,'') <> '' then @LineBreak + Address3 else '' end
  + case when isnull(PostalCode,'') <> '' then @LineBreak + PostalCode else '' end

Open in new window

0
 
LVL 3

Author Comment

by:Andy Green
ID: 39928607
I'm printing to a report engine where each address field has its own container. IE there are 4 text fields address1 - 4 & postcode.

I guess I could go with 1 field and use your solution.

Andy
0
 
LVL 15

Expert Comment

by:gplana
ID: 39928619
Maybe you can use different SELECTs with UNION:

SELECT IE, address1, address2, address3, PostalCode
FROM your_table
WHERE address1 IS NOT NULL AND address1<>''
     AND address2 IS NOT NULL AND address2<>''
    AND address3 IS NOT NULL AND address3<>''
UNION
SELECT IE, address1, address2, PostalCode, ''
FROM your_table
WHERE address1 IS NOT NULL AND address1<>''
     AND address2 IS NOT NULL AND address2<>''
    AND (address3 IS NULL OR address3='')
UNION
SELECT IE, address1, PostalCode,'',''
FROM your_table
WHERE address1 IS NOT NULL AND address1<>''
     AND (address2 IS NULL OR address2='')
    AND (address3 IS NULL OR address3='');

hope it helps.
0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 39928774
Thanks Guys. Have gone with this as it gives me more options. Able to drop a HTML field on the report and displays fine.

Andy
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question