"String or binary data would be truncated." 适用于 NVARCHAR 但不适用于 LIKE 操作中的 VARCHAR

"String or binary data would be truncated." for NVARCHAR but not VARCHAR in LIKE operation

在SQL服务器中,nvarchar占用varchar的space的两倍,与varchar相比,其前页指针限制为4000的 8000.

那么,为什么下面的like比较给出String or binary data would be truncated.错误...

select 1 where '' like cast(replicate('x', 4001) as nvarchar(max))

...而投射为更大的 varchar 不是吗?

select 1 where '' like cast(replicate('x', 123456) as varchar(max))

事实上,为什么top live在明确声明为nvarchar(max)的情况下会出现截断错误根本,其大小限制约为2GB?

来自 LIKE 运算符的 description

pattern

Is the specific string of characters to search for in match_expression, and can include the following valid wildcard characters. pattern can be a maximum of 8,000 bytes.

此查询显示符号的实际计数:

select  len(replicate('x', 123456)) as  CntVarchar,
        len(replicate('x', 4001))   as  CntNVarchar


+------------+-------------+
| CntVarchar | CntNVarchar |
+------------+-------------+
|       8000 |        4001 |
+------------+-------------+

第一种情况有8000字节。第二个有 8002 个字节,这违反了规则“最多可以有 8,000 个字节”。