Click here to Skip to main content
15,919,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to select model column form spare model table
where spare =1

Spare   Model
1       5
1       6
1       7
2       1  
2       8
2       6


result set come as given below

spare  Model
1       5,6,7


thanks
Posted
Updated 23-Aug-12 23:33pm
v4

SQL
DECLARE @Spare INT
DECLARE @combinedString NVARCHAR(MAX)

SET @Spare = 1 
SET @combinedString = ''

SELECT @combinedString = COALESCE(@combinedString + ', ', '') + Model
FROM SpareModelTable
WHERE Spare = @Spare


SELECT @Spare AS Spare, @combinedString AS Model
 
Share this answer
 
v3
Using SubQuery & xml path function
try this,
SQL
select distinct spare, (select model + ','  from tbl where spare=a.spare for xml path ('')) from tbl as a

Happy Coding!
:)
 
Share this answer
 
v2

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