3

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 cola values which are belongs to 2 to 3 days. And want to show that dates day into comma separated column as shown below in the expected ouptput.

Expected Output:

cola    Dates_Day
------------------
A       1,2,4
B       6,8
MAK
  • 6,824
  • 25
  • 74
  • 131

3 Answers3

3

Try this

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
1
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
Immu
  • 746
  • 5
  • 10
0
   select COLA, 
   listagg(TO_CHAR(COLDATE,'DD'),',') within group (order by COLDATE) AS CDAY
    from TEST_T
    group by COLA;
Pu297
  • 151
  • 8