[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL tidy up address data

Posted on 2014-03-14
4
Medium Priority
?
342 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.
Suggested Courses

864 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