0

I need to concatenate the address fields (zip,city,countryName, streetAddress) of my table. This is the query i wrote,

SELECT id,concat_ws(' ',address1,zip,city,(select countryName from country where countryCode = User.countryCode))  FROM User 

And it provides me 28 Avenue Pasteur 14390 Cabourg France which should actually be, 27 Avenue Pasteur, 14390 Cabourg, France (with commas separated)

How can i do achieve this?

Update when used SELECT id,concat_ws(',',address1,zip,city,(select countryName from country where countryCode = User.countryCode)) FROM User

it gives 27 Avenue Pasteur, 14390, Cabourg, France

but not 27 Avenue Pasteur, 14390 Cabourg, France (no comman in between 14390 Cabourg)

2
  • Use ', ' instead of ' ', if you want comma separators. Commented Jul 28, 2015 at 10:55
  • @GordonLinoff it gives me 28 Avenue Pasteur,14390,Cabourg,France but not 27 Avenue Pasteur, 14390 Cabourg, France Commented Jul 28, 2015 at 10:57

2 Answers 2

1

Try this way (also, it is better to use join):

select u.id,concat(u.address1,', ',u.zip,' ',u.city,', ',c.countryName) as Address
from User u join
     country c on u.countryCode=c.countryCode

The result will be:

27 Avenue Pasteur, 14390 Cabourg, France
Sign up to request clarification or add additional context in comments.

4 Comments

This isnt giving the address as output. SELECT u.id, u.address1 + ', ' + u.zip + ' ' + u.city + ', ' + c.countryName FROM User u JOIN country c ON u.countryCode = c.countryCode
The output is some numbers like 14418 under the column u.address1+', '+u.zip+' '+u.city+', '+c.countryName
@dev1234: Updated my answer. Try it now.
yes its working sorry. i was trying to sort out problem and forgot to accept the answer. stackoverflow.com/questions/31675410/…
1

You should add a comma as the delimiter of your string. Try this:

SELECT id,concat_ws(',',address1,zip,city,(select countryName from country where countryCode = User.countryCode))  FROM User;

1 Comment

I need the output as 27 Avenue Pasteur, 14390 Cabourg, France not as 27 Avenue Pasteur, 14390, Cabourg, France

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.