How to Create GUID With Non-Null Values
So far we learnt how we can check if the GUID is empty or not but how can we create values for the GUID if it is empty? again, we can simply do this by using the if statement again and if there is an empty GUID present then we will simply create the values for it.
Example
Query:
Declare @guidtest UniqueIdentifier
If(@guidtest IS NULL)
Begin
Set @guidtest = NEWID()
End
Select @guidtest
Output:
Note: The values will be different every time when we execute it because the GUID algorithm generates, new values every time it’s executed for uniqueness.
Explanation
As you can take guess, we are simply checking if the GUID is empty or not, and if the GUID is empty then we are simply using the set parameter and assigning a new value using the NEWID() function.
How to Check GUID is Null or Empty in SQL Server
GUID stands for Globally Unique Identifier, the GUID is a 16-byte long binary data type. the GUID is special because it is globally unique across all the databases, tables, and servers available. In the GUID there is a unique reference number present which is generated by the algorithm every time we execute the query to generate GUID. the GUID is stored as a 128-bit value but when it is displayed by the machine or system it is displayed as 32 hexadecimal digits. in this article we will learn what is GUID, why they are used as well as how to create them, and whether a GUID is null or empty in the SQL Server.