Hi @Aypn CNN ,
Here is an alternative method that would work for SQL Server version before 2017.
Additional functionality is that number of columns could vary. So the table could have DueDate6, ..., DueDateN columns.
SQL
-- DDL and sample data population, start
DECLARE @tbl Table (RowID Int IDENTITY PRIMARY KEY,DueDate1 int, DueDate2 int, DueDate3 int, DueDate4 int, DueDate5 int);
Insert INTO @tbl (DueDate1, DueDate2, DueDate3, DueDate4, DueDate5) VALUES
(1,0,3,0,5),
(1,0,0,4,0),
(1,2,3,4,5);
-- DDL and sample data population, end
SELECT p.RowID
, REPLACE(((
SELECT *
FROM @tbl AS c
WHERE c.RowID = p.RowID
FOR XML PATH(''), TYPE, ROOT('root')
).query('
for $x in /root/*[local-name()!="RowID"]/text()[. ne "0"]
return data($x)
').value('.', 'VARCHAR(50)'))
, SPACE(1), ' | ') AS Result
FROM @tbl AS p;
Output
+-------+-------------------+
| RowID | Result |
+-------+-------------------+
| 1 | 1 | 3 | 5 |
| 2 | 1 | 4 |
| 3 | 1 | 2 | 3 | 4 | 5 |
+-------+-------------------+