Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Combine address fields into one field in SQL

Posted on 2008-06-11
11
Medium Priority
?
903 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 2000 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 70

Expert Comment

by:Scott Pletcher
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

670 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