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]

Leave a Reply

Your email address will not be published. Required fields are marked *