Reputation: 2835
I'm getting a uniqueidentifier
into a Stored Procedure that looks like this
00000000-0000-0000-0000-000000000000
.
This seems like a simple thing, but how can identify that this is a blank uniqueidentifier
?
If I get a value like this DDB72E0C-FC43-4C34-A924-741445153021
I want to do X
If I get a value like this 00000000-0000-0000-0000-000000000000
I do Y
Is there a more elegant way then counting up the zeros?
Thanks in advance
Upvotes: 18
Views: 17323
Reputation: 51
Best solution is to use a constant for the empty GUID
DECLARE @EmptyGuid UNIQUEIDENTIFIER
SET @EmptyGuid = '00000000-0000-0000-0000-000000000000'
OR
DECLARE @EmptyGuid UNIQUEIDENTIFIER
SET @EmptyGuid = 0x0
and you just compare them
IF @parameter = @EmptyGuid
DO Y
ELSE
DO X
Note: you don't need to use casts and converts
Upvotes: 2
Reputation: 21
This also works.
DECLARE @EmptyGuid UNIQUEIDENTIFIER = CONVERT(UNIQUEIDENTIFIER, 0x0);
SELECT @EmptyGuid
Upvotes: 1
Reputation: 269438
IF (@TheGuid = '00000000-0000-0000-0000-000000000000')
SELECT 'Do Y'
ELSE
SELECT 'Do X'
Upvotes: 3
Reputation: 60508
Just create an EmptyGuid variable and compare against that:
DECLARE @EmptyGuid UniqueIdentifier
SET @EmptyGuid = '00000000-0000-0000-0000-000000000000'
Upvotes: 4