SQL Server 2008 R2: Display Date in Comma Separated Column
I have the following table with two fields:
create table test_t
(
cola varchar(10),
coldate date
);
Inserting some records:
insert into test_t values('A','1-1-2010'),
('A','2-1-2010'),
('A','4-1-2010'),
('B','6-1-2010'),
('B','8-1-2010'),
('C','10-1-2010'),
('D','11-1-2010');
Note . Now I want to show the values cola
that refer to 2 - 3 days. And want to show this day in a comma separated column as shown below in the expected ouptput.
Expected Output :
cola Dates_Day
------------------
A 1,2,4
B 6,8
+3
MAK
source
to share
3 answers
try it
select t1.cola, stuff((SELECT ',' + right(convert(varchar(5),t2.coldate,3),2) from
test_t t2 where t2.cola = t1.cola FOR XML PATH('')),1,1,'') AS Dates_Day
from test_t t1
group by t1.cola
+3
SimarjeetSingh Panghlia
source
to share
SELECT DISTINCT
COLA,
STUFF ((SELECT ','+ CONVERT(VARCHAR,RIGHT(LEFT(COLDATE,7),2)) FROM TEST_T B WHERE A.COLA = B.COLA FOR XML PATH('')),1,1,'') AS [Dates_Day]
FROM TEST_T A
+1
Immu
source
to share
select COLA,
listagg(TO_CHAR(COLDATE,'DD'),',') within group (order by COLDATE) AS CDAY
from TEST_T
group by COLA;
0
Pu297
source
to share