16

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?

1
  • 5
    Why it specifically talking about varchar is because ' | ' is a varchar literal. If you want an nvarchar literal, use N' | '. Commented Dec 27, 2012 at 13:26

4 Answers 4

28

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.

0
6

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.

4

To concatenate strings in MSSQL you should use +

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.