Question

I have variable of BINARY type and I am sending this value to another function to create a dynamic query for which I want this value to be concatenated and facing the error-

here is my code -

DECLARE @Bin BINARY =0x000002A2000001BC0001
        ,@SQL NVARCHAR(MAX)
SELECT @SQL = 'SELECT ' + @Bin + 'FROM TEST'

SELECT @SQL 

I get the following error when I run the above code -

Msg 402, Level 16, State 1, Line 3 The data types varchar and binary are incompatible in the add operator.

I need the concatenation and i don't want binary to be converted into string rather the value itself. I have searched extensively and haven't been able to get proper result. The closest I came to is this link http://www.sqlservercentral.com/Forums/Topic613703-338-1.aspx.

Any help on how to do this? I have also tried CAST and CONVERT but to no avail.

Was it helpful?

Solution

Please change the code to the following and check hope this helps.

SQL:

DECLARE @Bin BINARY(10)=0x000002A2000001BC0001,@SQL NVARCHAR(MAX)
SELECT @SQL = 'SELECT ' + CONVERT(nvarchar(max), @Bin, 1) + ' FROM TEST'
SELECT @SQL 

This returns the original values of the binary in the query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top