Dave Mateer
Dave Mateer

Reputation: 6626

SQL Server: WHERE clause to ignore blank GUID

SELECT ...
FROM ...
WHERE CASE WHEN @ActivityUID = '00000000-0000-0000-0000-000000000000' 
    THEN
        -- do nothing
    ELSE
        (Activity.ActivityUID = @ActivityUID)
    END

When a blank Guid is passed in via ActivityUID then I want the WHERE statement to ignore Activity.ActivityUID = @ActivityUID

Upvotes: 1

Views: 1209

Answers (2)

gbn
gbn

Reputation: 432210

This removes the need for an OR clause because NULL comparisons fail.

WHERE
    Activity.ActivityUID = NULLIF(@ActivityUID, '00000000-0000-0000-0000-000000000000')

See if there is a performance difference...

Upvotes: 0

geofftnz
geofftnz

Reputation: 10092

WHERE (
         Activity.ActivityUID = @ActivityUID OR 
         @ActivityUID = '00000000-0000-0000-0000-000000000000'
      )

Upvotes: 3

Related Questions