mike55 Posted September 28, 2005 Posted September 28, 2005 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. Quote 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)
mike55 Posted September 28, 2005 Author Posted September 28, 2005 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 Quote 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)
IxiRancid Posted September 28, 2005 Posted September 28, 2005 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? Quote
penfold69 Posted September 28, 2005 Posted September 28, 2005 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!) Quote
*Experts* Nerseus Posted September 29, 2005 *Experts* Posted September 29, 2005 @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 Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.