Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
Hi,
I want to select multiple columns into one single table column in sql server like,

| COLUMN 1 | COLUMN 2 | COLUMN 3 |          COLUMN 4             |
------------------------------------------------------------------
|      |          |          | SUB COL1 | SUB COL2 | SUB COL3|
|      |          |      |      |          |     |
|      |          |      |      |          |     |
|      |          |      |      |          |     |
|      |          |      |      |          |     |
|      |          |      |      |          |     |


In this figure Column 4 contains three sub columns. Is there any way to get the table result like this?
Posted
Comments
syed shanu 2-Sep-14 3:55am    
One simple way is like this :
Select T1.C1,t1.C2,T1.C3,T2.C1 +' | '+T2.C2+' | ' + T2.C3 as 'Col1 | Col2 | Col3'
from table1 T1
Left Outer Join
Table2 T2 ON T1.id=t2.id

provide more details about your requirement and your table details
Thanks7872 2-Sep-14 4:19am    
This is something related to how UI looks like. This should be handled at UI, not at back end.
Magic Wonder 2-Sep-14 4:23am    
Do you require same display at DB level?

1 solution

You can't divide a column into sub columns. As Rohan has said in the comments this should be addressed at the UI layer.

You should return result as
Column1 | Column2 | Column3 | Sub Column 1 | Sub Column 2 | Sub Column 3

and in the DataGrid you should apply a Template to show Sub Column 1 ... Sub Column 3 as Column 4
 
Share this answer
 
Comments
[no name] 23-Sep-14 3:16am    
Nice answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900