Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one table

like ex:

empcode date
4779 15-08-2015
4779 20-08-2015
1294 15-08-2015
6429 15-08-2015
5268 10-08-2015
5268 11-08-2015
5268 12-08-2015


i want data like

empcode date

4779 15,20
1294 15
6429 15
5268 10,11,12

empcode 4779 having nof days in one cell...pls tell me this very urgent..
Posted

If you want the days to be within a single column you could try something like the following

SQL
SELECT a.empcode,
       STUFF((SELECT ',' + RTRIM(datepart(day, datecol) ) 
              FROM mytable b 
              WHERE b.empcode = a.empcode 
              FOR XML PATH('')), 1, 1, '') AS dates
FROM (SELECT DISTINCT EmpCode FROM mytable) a
 
Share this answer
 
Comments
Member 11337367 28-Aug-15 6:06am    
one more i want that month under that i nee those values like in below format

empcode april,may,june
111 1,2 3,4 5,6
112 7,8 10 21,24
like this if it is possible pls help me out
Use Pivoting. Here is an example.

Simple Way To Use Pivot In SQL Query[^]
 
Share this answer
 
Comments
Member 11337367 25-Aug-15 1:50am    
please give query to make that..
Hi,

Use Stuff or Coalesce to get the same output that you want.

Please try with below query using STUFF.

SQL
SELECT DISTINCT empcode,
  STUFF((SELECT distinct ',' + Cast(Day([Date]) AS varchar)
         FROM [Table] p1
         WHERE p.empcode= p1.empcode
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'') as DateConcate
FROM [Table] p;


Please let me know if you have any concern or query on this or if you need more information on this.

Thanks
 
Share this answer
 
v2
Comments
Member 11337367 25-Aug-15 3:18am    
Can u pls explain your query since iam not able to understand that..what is the use of stuff and for xml u defined..pls know me what it does?

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