Click here to Skip to main content
15,672,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table name Item(Item_id,Item_name) table data like
1   BasicSalary 
2   Medical 
3   Loandry 
4   Transport   
5   Rent    
6   GrossSalary 
7   OverTime 

it can be more..

now the main point i need a query that can arrange it following way.

BasicSalary,Medical,Loandry,Transport, Rent,GrossSalary, OverTime 

because I need this for creating a new table.One thing this item can be more.

Any help will be appreciated gratefully.

1 solution

DECLARE @ConcatenatedValues NVARCHAR (MAX)
SELECT @ConcatenatedValues = COALESCE(@ConcatenatedValues + ',', '') + ColumnFromTable
FROM Table

SELECT @ConcatenatedValues
Share this answer
Mahmudunnabi 3-Jul-12 13:34pm    
nvarchar(max), and + ColumnFromTable can u please explain it.
ZurdoDev 3-Jul-12 13:39pm    
It works as a recursive function. So, you need somewhere to dump the data into, which is why you need to declare a variable to store it. Did you try it though?
Mahmudunnabi 3-Jul-12 13:50pm    
thanks a lot
Mahmudunnabi 3-Jul-12 14:08pm    
can u more helpful for me for giving more idea,like BasicSalary,Medical,Loandry,Transport,Rent,GrossSalary,OverTime,Lunch from how can i extract individual for treat single field & add data type double for creating new table.I think u understood what I am looking for.
ZurdoDev 3-Jul-12 14:53pm    
Sorry, I do not understand. Can you explain more clearly?

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