Skip to content

Instantly share code, notes, and snippets.

@dcs619
Created September 21, 2015 20:20
Show Gist options
  • Select an option

  • Save dcs619/2e959996b895d336a8e6 to your computer and use it in GitHub Desktop.

Select an option

Save dcs619/2e959996b895d336a8e6 to your computer and use it in GitHub Desktop.
Perform an optional join on NULL parameter
DECLARE @Today datetime = GETDATE()
DECLARE @CampusId int
SELECT @CampusId = 2
DECLARE @CampusGuid uniqueidentifier
SELECT @CampusGuid = '6246DDEF-8AC9-4C25-B011-430CEE590F0F'
SELECT COUNT(DISTINCT PersonAliasId)
FROM Attendance A
INNER JOIN (
-- Return a value (or return null)
SELECT (
SELECT ID
FROM Campus
WHERE GUID = @CampusGuid
) AS ID
) CampusFilter
ON A.CampusId = ISNULL(@CampusId, A.CampusId)
WHERE DidAttend = 1
-- Filter by Date Range
AND StartDateTime > DATEADD(month, -6, @Today)
AND StartDateTime < @Today
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment