Mych
Mych

Reputation: 2553

SQL Server datatypes nvarchar and varchar are incompatible error

I've inherited a C# app which I've converted to vb. I get one error which as far as I can see has nothing to do with the conversion.

I have a SQL statement which is....

SELECT   ResolverID AS ddlValue, ResolverTeam & ' | ' & ResolverPerson AS ddlText 
FROM     dbo.TblResolvers 
ORDER BY ResolverTeam, ResolverPerson;

When this runs I get the error:

The data types nvarchar and varchar are incompatible in the boolean AND operator.

In the table both ResolverTeam and ResolverPerson are specified as (nvarchar(255), null)

Why am I getting this error?

Upvotes: 16

Views: 45370

Answers (4)

Leonardo
Leonardo

Reputation: 11389

Try replacing the & for a +; by the looks of it, what you're trying to do is to concatenate 2 columns. Something you do need to be careful about is that nvarchar is double the size of regular varchar, which means there are chars in nvarchar that are not in the varchar table.

Upvotes: 28

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79919

You should use the + for string concatenation:

SELECT 
  ResolverID AS ddlValue, 
  ResolverTeam + ' | ' + ResolverPerson AS ddlText 
FROM dbo.TblResolvers 
Order By ResolverTeam, ResolverPerson;

Why am I getting this error?

You were getting that error, because of the & operator, which is the Bitwise AND.

Upvotes: 6

the_marcelo_r
the_marcelo_r

Reputation: 1856

Is this a concatenation attempt? ResolverTeam & ' | ' & ResolverPerson

& is the bitwise operator AND, replace it with + and see what happens.

Upvotes: 3

valex
valex

Reputation: 24134

To concatenate strings in MSSQL you should use +

SELECT ResolverID AS ddlValue, 
  ResolverTeam + ' | ' + ResolverPerson AS ddlText 
    FROM dbo.TblResolvers Order By ResolverTeam, ResolverPerson;

Upvotes: 4

Related Questions