codingguy3000
codingguy3000

Reputation: 2835

How do I identify a blank uniqueidentifier in SQL Server 2005?

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

Answers (5)

Gábor Lőrincz
Gábor Lőrincz

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

BrianB
BrianB

Reputation: 21

This also works.

DECLARE @EmptyGuid UNIQUEIDENTIFIER = CONVERT(UNIQUEIDENTIFIER, 0x0);  
SELECT @EmptyGuid

Upvotes: 1

LukeH
LukeH

Reputation: 269438

IF (@TheGuid = '00000000-0000-0000-0000-000000000000')
    SELECT 'Do Y'
ELSE
    SELECT 'Do X'

Upvotes: 3

Eric Petroelje
Eric Petroelje

Reputation: 60508

Just create an EmptyGuid variable and compare against that:

DECLARE @EmptyGuid UniqueIdentifier
SET @EmptyGuid = '00000000-0000-0000-0000-000000000000'

Upvotes: 4

davek
davek

Reputation: 22915

compare to

cast(cast(0 as binary) as uniqueidentifier)

?

Upvotes: 35

Related Questions