Hi guys, I've got the SQL code below. When the CompletionDate is 25/12/2022 the output is Q3/2022-2023, which is what I want.
But when the CompletionDate is 14/02/2023, the output is Q4/2023-2024. How do I make it so that the output is Q4/2022-2023? Thanks for any insight guys.
But when the CompletionDate is 14/02/2023, the output is Q4/2023-2024. How do I make it so that the output is Q4/2022-2023? Thanks for any insight guys.
Code:
'Q' + (
CASE WHEN Datepart(q, CompletionDate) = 1 THEN CAST(4 AS VARCHAR) + '/' + CAST(
Datepart(yyyy, CompletionDate) AS Varchar
) + '-' + CAST(
Datepart(
yyyy,
DATEADD(yyyy, 1, CompletionDate)
) AS Varchar
) WHEN Datepart(q, CompletionDate) = 2 THEN CAST(1 AS VARCHAR) + '/' + CAST (
Datepart(yyyy, CompletionDate) AS Varchar
) + '-' + CAST(
Datepart(
yyyy,
DATEADD(yyyy, 1, CompletionDate)
) AS Varchar
) WHEN Datepart(q, CompletionDate) = 3 THEN CAST(2 AS VARCHAR) + '/' + CAST(
Datepart(yyyy, CompletionDate) AS Varchar
) + '-' + CAST (
Datepart(
yyyy,
DATEADD(yyyy, 1, CompletionDate)
) AS Varchar
) WHEN Datepart(q, CompletionDate) = 4 THEN CAST(3 AS VARCHAR) + '/' + CAST(
Datepart(yyyy, CompletionDate) AS Varchar
) + '-' + CAST(
Datepart(
yyyy,
DATEADD(yyyy, 1, CompletionDate)
) AS Varchar
) END
) AS quarteryear