Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guid with extra characters issue

I have a table named Student, contain a column StudentId as GUID, so I used the Uniqueidentifier datatype for that.

If I want to get particular record, I get the result by the below query:

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

It returns the expected result. But in case if I mistakenly add any extra characters in the end also, it returns the same result. Like the below query:

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

If I pass the extra characters in the end of GUID, why it is not consider as invalid GUID? and return the same result?

like image 325
Muthukamatchi Ganesan Avatar asked Aug 07 '15 10:08

Muthukamatchi Ganesan


1 Answers

As stated from the documentation:

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

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

Here is the result set.

String                                       TruncatedValue
-------------------------------------------- ------------------------------------
0E984725-C51C-4BF4-9960-E1C80E27ABA0wrong    0E984725-C51C-4BF4-9960-E1C80E27ABA0

(1 row(s) affected)
like image 199
avi Avatar answered Oct 31 '22 14:10

avi