Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam extracting one data from database ..in that i want all days for particular value in one field like i have mentioned in below..please help me....

my query--

SQL
select dS.C_DSC_Code,D.C_FS_Code,DAY(D.D_Date_Report) from tbl_dwr d
join tbl_dwr_details ds on ds.N_Srno=d.N_Srno
join Tbl_Doc_Stock_Chem_Add_Mst sd on sd.C_Code=ds.C_DSC_Code
join Tbl_Cust_Div c on c.c_cust_code=sd.C_Code
where (('2015/10/01' between c.d_date and isnull(c.d_date_to,'2015/10/31'))
or
('2015/10/31' between c.d_date and isnull(c.d_date_to,'2015/10/31'))
)
and MONTH(d.D_Date_Report)=10 and YEAR(d.D_Date_Report)=2015 and sd.N_Type=1
and d.C_FS_Code='ME0001'
ORDER BY DS.C_DSC_Code


extracted data

C_DSC_Code	C_FS_Code	day
D000033778	ME0001	        15
D000033865	ME0001	        6
D000033865	ME0001   	23
D000033866	ME0001	        7
D000033866	ME0001	        16
D000033867	ME0001	        7
D000033867	ME0001	        16
D31863	        ME0001	        3
D31863	        ME0001	        14
D31863	        ME0001	        20
D31864	        ME0001    	3


I want in below format

C_DSC_Code	C_FS_Code	day
D000033778	ME0001	        15
D000033865	ME0001	        6,23
D000033866	ME0001	        7,16
D000033867	ME0001	        7,16
D31863	        ME0001	        3,14,20
D31864	        ME0001    	3


please tell as it is very urgent..
Posted
Updated 20-Nov-15 18:40pm
v2
Comments
PIEBALDconsult 21-Nov-15 0:43am    
What does that have to do with XML?
There are several articles about string concatenation here on CP; even _I_ have one: http://www.codeproject.com/Tips/301644/Stupid-CTE-tricks-string-concatenation
Member 11337367 21-Nov-15 1:06am    
How to alter this query i already written..
Member 11337367 21-Nov-15 1:16am    
select distinct code ,
STUFF((Select ','+days
from megh T1
where T1.code=T2.code
FOR XML PATH('')),1,1,'') from megh T2

Iam receving error for this

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value ',' to data type int.

PIEBALDconsult 21-Nov-15 11:43am    
That wasn't in your original post, please use Improve question to correct the question.

','+days -- won't work if days is an int so cast days to a string first.

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