[英]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. 我有一个名为
Student
的表,其中包含一列StudentId
作为GUID
,因此我为此使用了Uniqueidentifier
数据类型。
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
? 如果我在
GUID
的末尾传递了多余的字符,为什么不将其视为无效的GUID
? and return the same result? 并返回相同的结果?
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.
因为uniqueidentifier类型限制为36个字符,所以超过该长度的字符将被截断。
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)
GUID
is 16 bytes, so from the given 919C3BF9-B081-458C-897D-C0B3FF56AF73
GUID
是16个字节,因此从给定的919C3BF9-B081-458C-897D-C0B3FF56AF73
91
is 1st byte 91
是第一个字节
9C
is 2nd byte 9C
是第二个字节
3B
is 3rd byte 3B
是第3个字节
F9
is 4th byte F9
是第4个字节
.. ..
.. ..
.. ..
.. ..
56
is 14th byte 56
是第14个字节
AF
is 15th byte AF
是第15个字节
73
is 16th byte 73
是第16个字节
Parsing of 919C3BF9-B081-458C-897D-C0B3FF56AF73xyz
completes before the xyz
. 919C3BF9-B081-458C-897D-C0B3FF56AF73xyz
解析在xyz
之前完成。
So the characters are entered after the 16th byte, won't be consider. 因此,在第16个字节之后输入字符,将不予考虑。
But if you add any extra characters in the front, it won't consider as valid GUID
. 但是,如果在前面添加任何额外的字符,它将不会被视为有效的
GUID
。
Moreover, when you query with GUID
, use the code in between the {}
. 此外,当您使用
GUID
查询时,请使用{}
之间的代码。
SELECT * FROM Student
WHERE StudentId = '{919C3BF9-B081-458C-897D-C0B3FF56AF73}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.