1

Say for example we have the following table:

Address Line 1 | Address Line 2 | Address Line 3 | Town | Region | Postcode

Is there a way of selecting all rows from this and then returning the row as one string e.g

AddressLine1 + AddressLine 2 + AddressLine 3 etc.....

I apologise if this is a basic question, very new to SQL

3
  • 2
    Some form of string concatenation is supported in every RDBMS. MS SQL Server uses +, Oracle uses ||, MySQL uses CONCAT(). What RDBMS are you using? Commented Aug 10, 2012 at 13:54
  • Are these tables linked to each other via any relation, i mean is there any ForeignKey or PrimaryKey in these tables Commented Aug 10, 2012 at 13:55
  • @Michael i am using MS SQL Server 2008 Commented Aug 10, 2012 at 13:58

5 Answers 5

2

For MS SQL this is what I would use:

SELECT    ([Address Line 1] + ', ' + [Address Line 2] + ', ' + [Address Line 3] + ', ' 
          + Town + ', ' + Region + ', ' + Postcode) AS Address
FROM      TableName
Sign up to request clarification or add additional context in comments.

3 Comments

When i save the results as a .CSV from the output window and open in excel, they are still separated by columns? I removed the commas from your query too? Any ideas as to the issue?
@john I have just tried what you described, in fact I tried without any separators, just spaces and zero length separators, then saved as a .csv from the Results screen and opened in Excel 2007. Unfortunately I can't seem to replicate your issue. The only time I get the data separated into different columns is when I have commas as I do in the example I gave above!
@john, Just copy the result by right click and paste it to the excel file directly.
2

In oracle I used ||

Select AddressLine1 || ',' || AddressLine2 from address;

Syntax will be different in different database servers

1 Comment

The || is the standard concat operator but many DBMS support only proprietary operators, like +.
1

For MySQL you should use the CONCAT() function, so:

SELECT CONCAT( AddressLine1, ', ', AddressLine2, ', ', AddressLine3, ', ', Town, ', ', Region, ', ', Postcode ) AS LongAddress 
FROM ADDRESSES;

But if you want to avoid to add the ', ' between every field, you should use CONCAT_WS() instead:

SELECT CONCAT_WS( ', ', AddressLine1, AddressLine2, AddressLine3, Town, Region, Postcode ) AS LongAddress 
FROM ADDRESSES;

Comments

1

Try this: (SQL Server)

Please donot forget to use ISNULL() function, which just converts the column to '' if it is a null. If you do not do that , your entire row would be blank if any of the columns is null

select isnull([Address Line 1],'')+' '+
       isnull([Address Line 2],'')+' '+
       isnull([Address Line 3],'')+' '+
       isnull(Town,'') +' '+ 
       isnull(Region,'') +' '+ 
       isnull(Postcode,'')
from <table>

1 Comment

@podiluska: no.. I want to add a space after each column... :)
0

I realized the SQL-Server 2008 tag but just as an FYI, SQL Server 2012 implemented the CONCAT function so you could do something like CONCAT (AddressLine1, ',' , AddressLine2) and so one. And a good thing is that this function treats NULLS as empty strings so "Joe G Joseph" worries won't exist anynore on SQL 2012 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.