Solved

SQL tidy up address data

Posted on 2014-03-14
4
318 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now