SQL Server数据类型nvarchar和varchar是不兼容的错误

时间:2022-12-16 10:24:35

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.

我继承了一个c#应用程序,我将它转换为vb。我有一个错误,就我所能看到的,与转换没有任何关系。

I have a SQL statement which is....

我有一个SQL语句是....

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.

数据类型nvarchar和varchar在布尔和运算符中是不兼容的。

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

在表中,ResolverTeam和ResolverPerson都被指定为(nvarchar(255), null)

Why am I getting this error?

为什么会出现这个错误?

4 个解决方案

#1


17  

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

试着把‘&’换成‘+’……根据它的外观,你要做的是连接两列…有一件事你一定要小心:nvarchar是普通varchar的两倍大,这意味着nvarchar上的chars不在varchar表中……

#2


2  

You should use the + for string concatentation:

您应该使用+作为字符串表示:

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.

你会得到那个错误,因为&运算符,是位和。

#3


2  

To concatenate strings in MSSQL you should use +

要连接MSSQL中的字符串,应该使用+

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

#4


1  

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

这是连接尝试吗?分解团队和“|”和分解人

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

&是位运算符,用+替换,看看会发生什么。

#1


17  

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

试着把‘&’换成‘+’……根据它的外观,你要做的是连接两列…有一件事你一定要小心:nvarchar是普通varchar的两倍大,这意味着nvarchar上的chars不在varchar表中……

#2


2  

You should use the + for string concatentation:

您应该使用+作为字符串表示:

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.

你会得到那个错误,因为&运算符,是位和。

#3


2  

To concatenate strings in MSSQL you should use +

要连接MSSQL中的字符串,应该使用+

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

#4


1  

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

这是连接尝试吗?分解团队和“|”和分解人

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

&是位运算符,用+替换,看看会发生什么。