Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi

 

Need to select multiple columns in a row into a single variable, example - persons address: line 1, line 2, line3, town, county, country as myAddress.

 

Any suggestions?

 

Mike55.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted

Found a solution, not sure if it is the best solution, would appreciate it if some one could say it is or it isn't the best solution.

 

SELECT ( column1 + ', ' + column2 + ', ' + column3) as Exp1
FROM Table1

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted
What are you doing with the result then? If this is a VB or ASP.NET app then it's easy to read results into a string. However if you want to have a direct Expr in SQL statement then your way is a possibility. But perhaprs something could be done with Aliases?
Posted

SELECT CONCAT(column1, ", ", column2, ", ", ....) as Exp1
FROM Table1

 

This will NOT throw an error if one of the fields happens to be numerical (probably not in your case, but you never know!)

  • *Experts*
Posted

@mike55:

If you don't need the columns separated out for any reasons, I'd do it just the way you are, using + or whatever your DB uses for concatenation. You may want/need to wrap each column in case the values are null. In most databases, if you try to concatenate 2 strings and one is null, the result is null.

 

In SQL Server it would look like this:

SELECT IsNull(column1, '') + ', ' + IsNull(column2, '') + ', ' + IsNull(column3, '') as Exp1
FROM Table1

 

You can also use COALESCE (maybe with 2 L's?) instead of IsNull if you prefer.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...