Skip to content
Advertisement

SQL Server: Replace invalid XML characters from a VARCHAR(MAX) field

I have a VARCHAR(MAX) field which is being interfaced to an external system in XML format. The following errors were thrown by the interface:

We found that the following list of characters are invalid:

I am trying to clean this data, and I found a SQL function to clean these characters here. However, the function was taking NVARCHAR(4000) as input parameter, so I have changed the function to use VARCHAR(MAX) instead.

Could anyone please advise if changing the NVARCHAR(4000) to VARCHAR(MAX) would produce wrong results? Sorry, I wouldn’t be able to test this interface locally so thought to seek opinion/advise.

Original Function:

Modified Version:

Advertisement

Answer

It is safe to use VARCHAR(MAX) as my data column is a VARCHAR(MAX) field. Also, there will be an overhead of converting VARCHAR(MAX) to NVARCHAR(MAX) if I pass a VARCHAR(MAX) field to the SQL function which accepts the NVARCHAR(MAX) param.

Thank you very much @RhysJones, @Damien_The_Unbeliever for your comments.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement