Can someone help modify the query below to remove empty columns
For example, image you can column 'Last year end' is empty of values.
This table was produced with the following code.
SELECT
DatesTestv3.as_of_date
,CASE
WHEN DatesTestv3.as_of_date = EOMONTH(DATEADD(MONTH, -1, SYSDATETIME())) THEN 'LM'
END AS 'Last Month End'
,CASE
WHEN DatesTestv3.as_of_date = EOMONTH(DATEADD(QUARTER, -1, DATEFROMPARTS(YEAR(SYSDATETIME()), (MONTH(SYSDATETIME()) / 4 + 1) * 3, 1))) THEN 'LQ'
END AS [Last quarter end]
,CASE
WHEN DatesTestv3.as_of_date = DATEFROMPARTS(YEAR(SYSDATETIME()) - 1, 12, 31) THEN 'LY'
END AS [Last year end]
FROM dbo.DatesTestv3
I would like the query modified so that it removes empty columns such that the output would look like the following:
I have included the sample table
CREATE TABLE #tmpTable (
as_of_date date,
ColA varchar(50),
ColB varchar(50))
INSERT #tmpTable VALUES
(CONVERT(DATETIME, '2023-05-31', 120),'LM','A'),
(CONVERT(DATETIME, '2023-03-31', 120),'LQ','B'),
(CONVERT(DATETIME, '2021-12-31', 120),'LY','C'),
(CONVERT(DATETIME, '2023-04-30', 120),'LM2','D'),
(CONVERT(DATETIME, '2022-09-30', 120),'LQ2','E'),
(CONVERT(DATETIME, '2021-12-31', 120),'LY2','F')
SELECT * FROM #tmpTable
Thanks