Topics
Published on March 15, 2017 by kudvenkat

Text version of the video
csharp-video-tutorials.blogspot.com/2017/03/how-to-check-guid-is-null-or-empty-in.html

Slides
csharp-video-tutorials.blogspot.com/2017/03/how-to-check-guid-is-null-or-empty-in_15.html

All SQL Server Text Articles
csharp-video-tutorials.blogspot.com/p/free-sql-server-video-tutorials-for.html

All SQL Server Slides
csharp-video-tutorials.blogspot.com/p/sql-server.html

All SQL Server Tutorial Videos

All Dot Net and SQL Server Tutorials in English
www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
www.youtube.com/c/KudvenkatArabic/playlists

In this video we will discuss how to check if a GUID is null or empty

How to check if a GUID is NULL : Checking if a GUID is null is straight forward in SQL Server. Just use IS NULL keywords as shown below.

Declare @MyGuid Uniqueidentifier

If(@MyGuid IS NULL)
Begin
Print ‘Guid is null’
End
Else
Begin
Print ‘Guid is not null’
End

In the above example, since @MyGuid is just declared and not initialised, it prints the message “Guid is null”

Now let’s say, if a GUID variable is NULL, then we want to initialise that GUID variable with a new GUID value. If it’s not NULL, then we want to retain it’s value. One way to do this is by using an IF condition as shown below.

Declare @MyGuid UniqueIdentifier

If(@MyGuid IS NULL)
Begin
Set @MyGuid = NEWID()
End

Select @MyGuid

We can achieve exactly the same thing by using ISNULL() function. The advantage of using ISNULL() function is that, it reduces the amount of code we have to write.

Declare @MyGuid UniqueIdentifier
Select ISNULL(@MyGuid, NewID())

How to check if a GUID is EMPTY : Before understanding how to check if a GUID is empty, let’s understand what is an empty GUID. An empty GUID is a GUID with all ZEROS as shown below.
00000000-0000-0000-0000-000000000000

How to create this empty GUID. Do we have to type all the ZERO’s and Hyphens. The answe is NO. We do not have to type them manually. Instead use one of the following SELECT query’s to create an empty GUID. I prefer to use the second SELECT statement as it has only one CAST

SELECT CAST(CAST(0 AS BINARY) AS UNIQUEIDENTIFIER)
OR
SELECT CAST(0x0 AS UNIQUEIDENTIFIER)

To check if a GUID is an empty GUID, you have 2 options
Option 1: You can compare it to an Empty GUID value as shown below

Declare @MyGuid UniqueIdentifier
Set @MyGuid = ‘00000000-0000-0000-0000-000000000000’

If(@MyGuid = ‘00000000-0000-0000-0000-000000000000’)
Begin
Print ‘Guid is Empty’
End
Else
Begin
Print ‘Guid is not Empty’
End

Option 2: You can also compare it to a return value of the CAST method

Declare @MyGuid UniqueIdentifier
Set @MyGuid = ‘00000000-0000-0000-0000-000000000000’

If(@MyGuid = Cast(0x0 as Uniqueidentifier))
Begin
Print ‘Guid is Empty’
End
Else
Begin
Print ‘Guid is not Empty’
End

Leave a Reply

9 Comments on "How to check GUID is null or empty in SQL Server"

Notify of
avatar

Rishav Kumar
Guest
Rishav Kumar
30 days 8 hours ago

sir, I am a vivid learner of your tutorials… I request you to upload a tutorial on mvc with entity framework in latest visual studio 2013/2015 using all the required technologies like linq, ajax, jquery… And if possible please have a tutorial on a basic project related to these technologies…Thank you Sir.

Abhay Kumar
Guest
Abhay Kumar
1 month 4 days ago

Hi,I am looking some DBA related tutorial..can you help me

jimbofatplum
Guest
jimbofatplum
1 month 6 days ago
hi there,i know this is probably in the wrong place but as your a master at c# i wonder if you could you help me here?i want to draw a line 20 pixels long. i want this line to start from the centre of a panel [x=0;y=0]. i want this line drawn on an angle of say 15 degs. i know i need to use the sin and cos functions but im stuck on how to add the length of line to this.also, 🙂 by making the origin 0,0 in centre of panel this will involve using quadrants of neg… Read more »
Kushal Mazumder
Guest
Kushal Mazumder
1 month 8 days ago

It would be helpful if you can share some videos about SSRS/SSIS/SSAS.Thanks

Srsh s
Guest
Srsh s
1 month 8 days ago

can u help me… if insert grid view and connect SQL database to the grid view… data are getting. if add one field textbox or check box…. those data can be save in database…. plz tell me how to do this…. reply plz.

Hosam Nima
Guest
Hosam Nima
1 month 8 days ago

The best tutorial for SQL server. So thanks

Sweetcracks
Guest
Sweetcracks
1 month 8 days ago

r u making videos on any new programming language & when u gng to upload that videos..? thank you i love ur channel…!!!

Carmel Raj
Guest
Carmel Raj
1 month 8 days ago

You are the best Teacher always. I learn a lot, just because of your video tutorials. Thanks a lot

Muhammad Rehbar Sheikh
Guest
Muhammad Rehbar Sheikh
1 month 9 days ago

Best explanation. Thanks venkat sir.

wpDiscuz