Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checking the difference between isnull and coalesce
DECLARE @Date date = DATEADD(DAY, -1, GETDATE());
SELECT SpentDate,
SUM(SpentAmount) AS Total
FROM dbo.RunningTotals
WHERE SpentDate >= ISNULL(@Date, GETDATE())
GROUP BY
GROUPING SETS(
SpentDate, -- Per date,
() -- To include the overall total as well.
);
GO
DECLARE @Date date = NULL;
SELECT SpentDate,
SUM(SpentAmount) AS Total
FROM dbo.RunningTotals
WHERE SpentDate >= ISNULL(@Date, GETDATE())
GROUP BY
GROUPING SETS(
SpentDate,
()
);
GO
DECLARE @Date date = DATEADD(DAY, -1, GETDATE());
SELECT SpentDate,
SUM(SpentAmount) AS Total
FROM dbo.RunningTotals
WHERE SpentDate >= COALESCE(@Date, GETDATE())
GROUP BY
GROUPING SETS(
SpentDate,
()
);
GO
DECLARE @Date date = NULL;
SELECT SpentDate,
SUM(SpentAmount) AS Total
FROM dbo.RunningTotals
WHERE SpentDate >= COALESCE(@Date, GETDATE())
GROUP BY
GROUPING SETS(
SpentDate,
()
);
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment