Skip to content

Instantly share code, notes, and snippets.

@shaneis
Last active October 9, 2017 12:47
Show Gist options
  • Save shaneis/1732896fbeeca3499792495e4aacb49e to your computer and use it in GitHub Desktop.
Save shaneis/1732896fbeeca3499792495e4aacb49e to your computer and use it in GitHub Desktop.
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