Solved

SQL tidy up address data

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

929 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

9 Experts available now in Live!

Get 1:1 Help Now