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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14055400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:50:30  来源:igfitidea点击:

SQL Server datatypes nvarchar and varchar are incompatible error

sqlsql-servertsql

提问by Mych

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 在布尔 AND 运算符中不兼容。

In the table both ResolverTeamand ResolverPersonare specified as (nvarchar(255), null)

在表中,ResolverTeamResolverPerson都指定为 ( nvarchar(255), null)

Why am I getting this error?

为什么我收到这个错误?

回答by Leonardo

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 nvarcharis double the size of regular varchar, which means there are chars in nvarcharthat are not in the varchartable.

尝试替换&for a +; 从它的外观来看,您要做的是连接 2 列。您需要注意的是它nvarchar是常规大小的两倍varchar,这意味着其中nvarchar有不在varchar表中的字符。

回答by Mahmoud Gamal

You should use the +for string concatenation:

您应该使用+for 字符串连接:

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.

由于&运算符,您收到了该错误,即按位 AND

回答by valex

To concatenate strings in MSSQL you should use +

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

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

回答by the_marcelo_r

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

这是连接尝试吗? ResolverTeam & ' | ' & ResolverPerson

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

&是按位运算符AND,将其替换为+,看看会发生什么。