Msg 402 Level 16 State 1 Line 5 The data types varchar(max) and xml are incompatible in the add operator. – SQLSERVERLEARNER https://sqlserverlearner.com LEARN SQL SERVER ONLINE Wed, 16 May 2012 11:06:57 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 The data types varchar(max) and xml are incompatible in the add operator https://sqlserverlearner.com/2012/05/16/the-data-types-varcharmax-and-xml-are-incompatible-in-the-add-operator/ https://sqlserverlearner.com/2012/05/16/the-data-types-varcharmax-and-xml-are-incompatible-in-the-add-operator/#respond Wed, 16 May 2012 11:06:57 +0000 https://sqlserverlearner.azurewebsites.net/2012/05/16/the-data-types-varcharmax-and-xml-are-incompatible-in-the-add-operator/ Error Message:

Msg 402, Level 16, State 1, Line 5
The data types varchar(max) and xml are incompatible in the add operator.

This message occurs when you try to add varchar and xml data types.

Fix:
If you wanted to concatinate the XML Data type and Varchar Data type values, use the CAST Operator on XML Data type to convert into VARCHAR Datatype.

Example:
[sql]
DECLARE @a XML
DECLARE @b VARCHAR(max)
SET @a = ‘<A></A>’
SET @b = ‘ABC’
SELECT @b + @a
[/sql]
The Above code Gives Error 402.

Fixed Code:
[sql]
DECLARE @a XML
DECLARE @b VARCHAR(max)
SET @a = ‘<A></A>’
SET @b = ‘ABC’
SELECT CAST(@a AS VARCHAR(max))+@b
[/sql]

]]>
https://sqlserverlearner.com/2012/05/16/the-data-types-varcharmax-and-xml-are-incompatible-in-the-add-operator/feed/ 0