Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

for my application

i need to write a function that returns more than one value with commaseperated

by giving id

ex:output: 1,2,3,4,5,6------
Posted
Comments
NAPorwal(8015059) 7-Nov-12 6:07am    
ur ques is not clear enough, will this value get fetched from database on the basis of your passed id or something else do u want?
sk. maqdoom ali 7-Nov-12 6:16am    
yes, i want to fetch the combination

May be helpful to you.

SQL
Select STUFF((SELECT ' , '+ LRT.LRNo
        FROM TRNS.LorryReceipt LRT
        FOR XML PATH('')), 1, 2, '') AS [LRNo]
 
Share this answer
 
Comments
damodara naidu betha 7-Nov-12 6:56am    
5+
I am assuming you have parent-child tables and you are trying to get all matching children for a given parent table ID, try this function below:

SQL
CREATE FUNCTION GetCSVId(@IdParent int)	
	RETURNS VARCHAR(1000)	
AS
BEGIN	
	DECLARE csv VARCHAR(1000)
	
	SELECT @csv =COALESCE(@csv+',','') + value
	from CHILDTABLE
	WHERE IDParent = @IdParent
	
	RETURN @csv
END
 
Share this answer
 
v2
Comments
damodara naidu betha 7-Nov-12 6:56am    
5+
Try:
SQL
SELECT SUBSTRING(
(SELECT ',' + CONVERT(varchar, myValue) FROM myTable WHERE Id=666
FOR XML PATH('')),2,100000) AS CSV
 
Share this answer
 
SQL
CREATE FUNCTION CSVSTRING(@Id INT)
RETUNRNS VARCHAR(1000)
BEGIN
DECLARE @Result VARCHAR(1000);
SELECT @Result = STUFF((SELECT ' , '+ COLUMN
        FROM MYTABLE WHERE IDCOLUMN = @Id
        FOR XML PATH('')), 1, 2, '') AS 

RETURN @Result
END
 
Share this 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