The SQL table I have is as follows:
ID Name NameID
1 John 1
1 Dan 2
1 Jill 3
2 Jack 4
2 Sam 5
I need this table to be like this:
ID Name NameID
1 John, Dan, Jill 1, 2, 3
2 Jack, Sam 4, 5
The SQL query that I have for now is as follows:
SELECT ID,
STUFF((
SELECT N', ' + Name
FROM Table2 b
WHERE b.NameID = a.NameID
FOR XML PATH (''),TYPE).value('text()[1]','nvarchar(max)'
),1,2,N'') AS Name,
NameID
FROM Table1 a
That query does what I need for the Name column but it won't work when I try to do it for the NameID column. I need all the cells that have int to remain as int. I cannot have them as string since I will be needing to filter the table by the NameID. How do I get all NameID into a single cell for each ID?
intvalues onto a single column, but you want them to remain asints?I need all the cells that have int to remain as int- Why? What good will it do to keep the column type as int (which is not possible)? Could you execute math operation on it or add them together or something else (rhetorical question, answer is no). Those are reasons to keep a value numeric. Now for the sake of argument lets assume it is possible, a concatenated list of ints in a single cell is useless in sql, you can't do anything with it or materialize it out of sql (lets say to an ORM).Names andNameIDs and when a user chooses an option, it will filter the grid with above information by theNameID. If there is a way to still filter byNameIDand have it as a string, then please show me how. Any help is much appreciated.