Nvarchar and text are incompatible in the add operator

Petty picture Petty · Sep 10, 2013 · Viewed 29.1k times · Source

With SQL Server 2008, I'd like to add a variable (@wfID) into a text, like below:

 DECLARE @wfID uniqueidentifier
 SET @wfID = NEWID()

'<META http-equiv="Content-Type" content="text/html; " />  <br><input type="button"
    value="" onclick="window.open(&quot;http://localhost/TestWeb2/Test_Look.aspx?Test_ID='
    + convert(nvarchar, @wfID)
    + '&quot;);" /></br>',

So, I'd like to add the @wfID to the text but it always says

The data types nvarchar and text are incompatible in the add operator.

I tried converting everything into nvarchar, but then I got this:

Arithmetic overflow error converting expression to data type nvarchar.

Any suggestions?

Answer

krisku picture krisku · Sep 10, 2013

In your call to convert you try to convert to just nvarchar which implicitly means nvarchar(1), i.e. there is not room for the whole guid to be converted.

Change that to

convert(nvarchar(36), @wfID)

and it will work.

For some strange reason MSSQL gives an arithmetic overflow if doing SELECT CONVERT(nvarchar,NEWID()) but gives a proper 'Insufficient result space' if doing SELECT CONVERT(varchar,NEWID()).