Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for empty GUID in SQL

How do I check if an argument in a stored procedure is an empty GUID or not?

like image 463
hovkar Avatar asked Jun 22 '10 12:06

hovkar


People also ask

How do I find my SQL GUID?

-- If you want to generate a new Guid (uniqueidentifier) in SQL server the you can simply use the NEWID() function. -- This will return a new random uniqueidentifier e.g. You can directly use this with INSERT statement to insert new row in table.

What is an empty GUID?

You can use Guid.Empty . It is a read-only instance of the Guid structure with the value of 00000000-0000-0000-0000-000000000000. you can also use these instead. var g = new Guid(); var g = default(Guid);


1 Answers

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

That should return your empty guid.

... or even shorter, saving one cast:

SELECT CAST(0x0 AS UNIQUEIDENTIFIER) 

So to check for that, you would do

IF @GuidParam = CAST(CAST(0 AS BINARY) AS UNIQUEIDENTIFIER) BEGIN    --Guid is empty END 
like image 144
Meiscooldude Avatar answered Oct 07 '22 06:10

Meiscooldude