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

5

18

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

Kornher answered 18/12, 2009 at 22:1 Comment(0)
E
35

compare to

cast(cast(0 as binary) as uniqueidentifier)

?

Epsilon answered 18/12, 2009 at 22:6 Comment(1)
even shorter: CAST(0x0 AS UNIQUEIDENTIFIER), shorterer: CAST(0x AS UNIQUEIDENTIFIER)Hammad
V
4

Just create an EmptyGuid variable and compare against that:

DECLARE @EmptyGuid UniqueIdentifier
SET @EmptyGuid = '00000000-0000-0000-0000-000000000000'
Veta answered 18/12, 2009 at 22:11 Comment(0)
W
3
IF (@TheGuid = '00000000-0000-0000-0000-000000000000')
    SELECT 'Do Y'
ELSE
    SELECT 'Do X'
Wolfish answered 18/12, 2009 at 22:12 Comment(0)
W
2

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

Wichita answered 13/11, 2015 at 14:38 Comment(0)
U
1

This also works.

DECLARE @EmptyGuid UNIQUEIDENTIFIER = CONVERT(UNIQUEIDENTIFIER, 0x0);  
SELECT @EmptyGuid
Unemployable answered 11/11, 2014 at 14:25 Comment(1)
The question was on SQL-Server 2005 yours will work from 2008 up.Conk

© 2022 - 2024 — McMap. All rights reserved.