Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In database records as follows

Schdate Course Session Faculty
28 Aug MFA 1 AD
28 AUg EFA 2 AD
28 Aug MC 3 AD

i want the output as follows

28Aug(MFA -S1,EFA -S2,MC -S3)

for getting a above output i tried my query as follows

SQL
SELECT Facuty, 
  STUFF((SELECT '( ' + Convert(char(7),[Schdate],100 )+ ' ' + [Course] + '-S' + [Session] +  ')'
              from Tb_Sch_Time_Table_Details sch
            where  sch.Facuty = sch1.Facuty
             FOR XML PATH('') [course])
        FROM Tb_Sch_Time_Table_Details sch1
GROUP BY Facuty
ORDER BY 1



When i execute the above query shows error as follows

Incorrect syntax near 'Course'

please help me how can i solve the problem to get the correct ouptut.
Posted
Updated 24-Aug-14 6:56am
v2
Comments
BacchusBeale 24-Aug-14 19:58pm    
I never used it but looking at MSDN syntax is wrong: STUFF ( character_expression , start , length , replaceWith_expression ) http://msdn.microsoft.com/en-au/library/ms188043.aspx
David Lee 145 24-Aug-14 21:32pm    
Is your XML PATH right? Can you show me the full source data?

Just try the below query and let me know the result.

SQL
select Convert(char(7),sch1.[Schdate],100 )+'('+STUFF(
(select ','+ ltrim(rtrim([Course])) +'-S'+ltrim(rtrim([Session])) from Tb_Sch_Time_Table_Details sch
where sch.Schdate = sch1.Schdate
FOR XML PATH(''))
,1,1,'')+')' from Tb_Sch_Time_Table_Details sch1
group by Schdate
order by 1
 
Share this answer
 
Try this

SELECT Faculty, 
  (SELECT '( ' + Convert(char(7),[Schdate],100 )+ ' ' + [Course] + '-S' + cast([Session] as varchar(5) )+  ')'
              from @TBL sch
            where  sch.Faculty = sch1.Faculty
             FOR XML PATH('') )
        FROM @TBL sch1
GROUP BY Faculty
ORDER BY 1
 
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