Extra Character Guide

I have a table called Student containing the StudentId column as a GUID , so I used the Uniqueidentifier data type for this.

If I want to get a specific record, I get the result by the following query:

 SELECT * FROM Student WHERE StudentId = '919C3BF9-B081-458C-897D-C0B3FF56AF73' 

It returns the expected result. But in case I mistakenly add extra characters at the end, it also returns the same result. As in the following query:

 SELECT * FROM Student WHERE StudentId = '919C3BF9-B081-458C-897D-C0B3FF56AF73xyz' 

If I pass extra characters at the end of the GUID , why is it not considered an invalid GUID ? and return the same result?

+5
source share
2 answers

GUID is 16 bytes, so from this 919C3BF9-B081-458C-897D-C0B3FF56AF73

91 - 1st byte
9C - second byte
3B - third byte
F9 - 4th byte
..
..
..
..
56 - 14th byte
AF - 15th byte
73 - 16th byte

Analysis 919C3BF9-B081-458C-897D-C0B3FF56AF73xyz completed before xyz .

So, characters entered after the 16th byte are not considered.

But if you add extra characters in front, this will not be considered a valid GUID .

Also, when you request using the GUID , use the code between {} .

 SELECT * FROM Student WHERE StudentId = '{919C3BF9-B081-458C-897D-C0B3FF56AF73}' 
+2
source

As stated in the documentation :

The following example demonstrates data truncation when the value is too large for the converted data type to be converted. Because the uniqueidentifier type is limited to 36 characters, characters that exceed this length are truncated.

 DECLARE @ID nvarchar(max) = N'0E984725-C51C-4BF4-9960-E1C80E27ABA0wrong'; SELECT @ID, CONVERT(uniqueidentifier, @ID) AS TruncatedValue; 

Here is the result.

 String TruncatedValue -------------------------------------------- ------------------------------------ 0E984725-C51C-4BF4-9960-E1C80E27ABA0wrong 0E984725-C51C-4BF4-9960-E1C80E27ABA0 (1 row(s) affected) 
+3
source

All Articles