How to check whether GUID is null or empty?

Before going to look at this i suggest you to see GUID part 1 and part 2.

                                                         GUID part - 1

                                                         GUID part - 2

To check whether the given GUID is null, in SQL server, we have IS NULL keyword.

Declare @blog Uniqueidentifier

If(@blog IS NULL)
Begin
     Print 'The @blog Guid is null'
End
Else
Begin
     Print 'The @blog Guid is not null'

End


Where @blog is the variable. If we execute the above query obviously it prints 'The @blog Guid is null'

Now set the value to the variable @blog as follows.

Declare @blog Uniqueidentifier
set @blog=NEWID()
If(@blog IS NULL)
Begin
     Print 'The @blog Guid is null'
End
Else
Begin
     Print @blog

End


If we execute the above query obviously it prints the GUID value such as ' 17E2A682-F302-45BF-89E6-E46C0513D5D5' and if we you execute the query GUID value will be changed.

We can do the same thing as follows.

Declare @blog Uniqueidentifier

If(@blog IS NULL)
Begin
     set @blog=NEWID()
End


select @blog


--------------------------------------------------------------------------------------------------------------------------
Have a look at the following query.

Declare @blog Uniqueidentifier

select ISNULL(@blog, NEWID())

In the above query we know that @blog is null then NEWID() will be returned and the result will be as '5A5AC20F-4D51-4A98-BF21-5AEB6FA05148'  and it will be changed if you reexecute.


If you execute the query by giving value to the variable as follows, then @blog variable result will be returned,

Declare @blog Uniqueidentifier
set @blog='5A5AC20F-4D51-4A98-BF21-5AEB6FA05148'

select ISNULL(@blog, NEWID())

--------------------------------------------------------------------------------------------------------------------------

Empty GUID:

Generally empty GUID will be with all zero's as follows.


00000000-0000-0000-0000-000000000000

To get an empty GUID we need to use select statement as follows.

1) SELECT CAST(CAST(0 AS BINARY) AS UNIQUEIDENTIFIER)
2) SELECT CAST(0x0 AS UNIQUEIDENTIFIER)

We can use any one of the above. The following are the two scenarios to check the GUID is empty.

1) By comparing with empty.


Declare @blog UniqueIdentifier
Set @blog = '00000000-0000-0000-0000-000000000000'

If(@blog = '00000000-0000-0000-0000-000000000000')
Begin
     Print 'The @blog Guid is Empty'
End
Else
Begin
     Print 'The @blog Guid is not Empty'
End


In the above scenario the result is that 'The @blog Guid is Empty'

2) By using CAST 


Declare @blog UniqueIdentifier
Set @blog = '00000000-0000-0000-0000-000000000000'

If(@blog = Cast(0x0 as Uniqueidentifier))
Begin
     Print 'The @blog Guid is Empty'
End
Else
Begin
     Print 'The @blog Guid is not Empty'
End


In the above scenario the result is that 'The @blog Guid is Empty'






1 comment:

  1. The latest example can be rewritten as below (we are talking about T-SQL, right?):

    Declare @blog UniqueIdentifier
    Set @blog = '00000000-0000-0000-0000-000000000000'

    If(@blog = 0x0)
    Begin
        Print 'The @blog Guid is Empty'
    End
    Else
    Begin
        Print 'The @blog Guid is not Empty'
    End

    Please note 0x0 is the hexadecimal equivalent of the empty guid.

    ReplyDelete

What is Normalization?

Database normalization is a data design and organization process applied to data structures based on rules that help building relational dat...