Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi, for my application

i have a table with two columns ID and Applicant_Member_Id having data as

ID Applicant_Member_Id

117 1
117 2
186 3
186 4
223 5
224 6
556 7
556 8
186 9
-
-
-
i want a function that which returns the output as
117 1,2
186 3,4
556 7,8
223 5
224 6
Posted
Updated 7-Nov-12 17:41pm
v2

There is not a function for this
you can use query that will combine value using FOR XML PATH('') Inbuilt Function of SQL
SQL
select ID, substring(Applicant_Member_Ids,1,len(Applicant_Member_Ids)-1) from
(
    select ID ,
    (select convert(varchar, Applicant_Member_Id) + ','  as [text()] from YourTable where ID =a.ID  for xml path(''))  as Applicant_Member_Ids
    from YourTable a
    group by ID
) as temp

Happy Coding!
:)
 
Share this answer
 
v2
Comments
damodara naidu betha 7-Nov-12 23:58pm    
5+
Aarti Meswania 7-Nov-12 23:59pm    
Thank you!
:)
sk. maqdoom ali 8-Nov-12 0:13am    
thank u,
For my banking application i will pass many number of Applicant_Member_Id 's and my requirement is if i selected 41,42 for one JointAccount then for another JointAccount i will restrict the same combination which i have given before ie,41,42
i can use 41,43 or 42,43 like this.
i want to restrict the combination which is already repeated
Aarti Meswania 8-Nov-12 0:19am    
yes you can
if you mean 41,42 should not repeat
then use Row_Number function over Applicant_Member_Ids and display Which row_Number is equal to 1
sk. maqdoom ali 8-Nov-12 0:43am    
i written this query but i am getting the Row_Number which shows invidually not by combination ,
SELECT
ROW_NUMBER() over(order by id) AS Row, Applicant_Member_Id ,ID
FROM Savings_Account_Applicant where Applicant_Member_Id in(39,51)
Try this...
SQL
DECLARE @ApplicantTable TABLE
(
  ID INT,
  ApplicantId INT
)

INSERT INTO @ApplicantTable VALUES
(117,1),
(117 ,2),
(186 ,3),
(186 ,4),
(223, 5),
(224, 6),
(556, 7),
(556, 8),
(186, 9)

SELECT ID,STUFF(( SELECT ',' + CAST(ApplicantId AS VARCHAR) FROM @ApplicantTable 
WHERE ID = A.Id FOR XML PATH('') ),1,1,'')Ids
FROM @ApplicantTable A GROUP BY ID


And to get the rownumber according to the combination....
SQL
SELECT ID,ROW_NUMBER() OVER(ORDER BY Ids) RowNum,IDs FROM (
SELECT ID,STUFF(( SELECT ',' + CAST(ApplicantId AS VARCHAR) FROM @ApplicantTable 
WHERE ID = A.Id FOR XML PATH('') ),1,1,'')Ids
FROM @ApplicantTable A GROUP BY ID ) A

Thank you
 
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