Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL tidy up address data

Posted on 2014-03-14
4
Medium Priority
?
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

610 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