Solved

Combine address fields into one field in SQL

Posted on 2008-06-11
11
895 Views
Last Modified: 2008-06-22
I have 4 fields in My table Name, Addr1, Addr2, Addr3.

e.g.
Name:          J Smith
Addr1          37 Main Street
Addr2          Auckland
Addr3          Null

I would like to combine these into one field that reads:-

J Smith, 37 Main Street, Auckland

Is there a way to do this with a case statement it function - I would like to select this table and list one field in the format above.
0
Comment
Question by:donhannam
  • 2
  • 2
  • 2
  • +4
11 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 21757733
(name + ' ' +Addr1 +' ' etc)
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 21757737
whoops forgot (name + ' ' +Addr1 +' ' etc) as name_address
0
 
LVL 8

Expert Comment

by:matrix_aash
ID: 21757744
You can achieve it witout CASE Statement by concatenating the fields as follows:

Select NAME + ',' + ' ' + Addr1 + ' ' + Addr2 +',' +  ' ' + Addr3 From Tablename

Hope this helps

Cheers.
0
 
LVL 5

Expert Comment

by:rstomar
ID: 21757802
Select Name + COALESCE(', ' + Addr1,'') + COALESCE(', ' + Addr2,'')  + COALESCE(', ' + Addr3,'')
from TableName
0
 
LVL 2

Expert Comment

by:howyue
ID: 21758206
i would come out something similar to rstomar solution which take cares of null value and comma seperator altogether.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:donhannam
ID: 21758441
OK - can just concatenate but I have a large number of records - some with no values in so to create this statement I need to do something to put the comma in the right place.

i.e. if first field null or '' then don't put a comma after first field - If any other field is null or '' then don't put a comma before it.
0
 
LVL 8

Assisted Solution

by:matrix_aash
matrix_aash earned 500 total points
ID: 21758462

Select NAME + ',' + ' ' + Addr1 + ' ' + Addr2 +',' +  ' ' + Addr3,
CASE WHEN NAME IS NULL THEN (NAME + ' ' + Addr1 + ' ' + Addr2 +',' +  ' ' + Addr3),
CASE WHEN ADDR1 IS NULL THEN (NAME + ',' + ' ' + Addr1 + ' ' + Addr2 +',' +  ' ' + Addr3),...SO ON
ELSE ''
END AS O
 From Tablename
0
 
LVL 2

Expert Comment

by:MichealMcLoughlin
ID: 21758635
Select Replace(Name + ',' + Coalesce(Addr1,'') + ',' + coalesce(Addr2,'') + ',' +  Coalesce(Addr3,'') + ',' + Coalesce(Addr4,''),',,','')  'Address' From MyTable
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 21762004
Create a computed column -- one time -- on the table, like so:

ALTER TABLE tablename
    ADD NameAndAddress AS Name + COALESCE(', ' + Addr1, '') + COALESCE(', ' + Addr2, '') + COALESCE(', ' + Addr3, '')

Then use that new name like any other column, for example:

SELECT NameAndAddress
FROM ...
WHERE NameAndAddress LIKE '%...%'
ORDER BY NameAndAddress

The code assumes:
1) name is always present
2) a missing address value will be NULL not '' (empty).

If either of those are wrong, pls let me know.
0
 
LVL 5

Expert Comment

by:rstomar
ID: 21762519
thats what my solution does......did you try it
0
 

Accepted Solution

by:
donhannam earned 0 total points
ID: 21764668
Thanks for suggestions. I have had another think through and have come up with the statement below.

SELECT     Name + CASE WHEN Name IS NULL OR
                      Name = '' OR
                      Addr1 IS NULL OR
                      Addr1 = '' THEN '' ELSE ', ' END + Addr1 + CASE WHEN Addr2 IS NULL OR
                      Addr2 = '' THEN '' ELSE ', ' END + Addr2 + CASE WHEN Addr3 IS NULL OR
                      Addr3 = '' THEN '' ELSE ', ' END + Addr3 + CASE WHEN Addr4 IS NULL OR
                      Addr4 = '' THEN '' ELSE ', ' END + Addr4 + CASE WHEN Addr5 IS NULL OR
                      Addr5 = '' THEN '' ELSE ', ' END AS LongAddress
FROM         Cust_Table

This seems to work OK. In the examples given Coalesce and Replace where used. I would be interested to know the purpose of using these.

0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

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 …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

895 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

12 Experts available now in Live!

Get 1:1 Help Now