[英]What's the right way to compare an NTEXT column with a constant value?
If I use something like 如果我使用类似的东西
[ntext2] <> '1,032.5',
I get this error: 我收到此错误:
The data types ntext and varchar are incompatible in the not equal to operator.
数据类型ntext和varchar在不等于运算符时不兼容。
The best possible solution would be if comparison is implemented in the same way for any column type. 最好的解决方案是,如果对任何列类型以相同的方式实现比较。 (<> operator is applicable for both NVARCHAR and INT).
(<>运算符适用于NVARCHAR和INT)。
The ntext
data type is deprecated in favour of the nvarchar(max)
data type. 不推荐使用
ntext
数据类型,而使用nvarchar(max)
数据类型。 If you can change the data type in the table, that would be the best solution. 如果您可以更改表中的数据类型,那将是最佳解决方案。 Then there is no problem comparing it to a
varchar
literal. 然后将它与
varchar
文字进行比较没有问题。
Otherwise you would have to cast the value before comparing it: 否则你必须在比较之前转换值:
cast([ntext2] as nvarchar(max)) <> '1,032.5'
You might also consider using a nvarchar literal, which solves some similar data type problems: 您还可以考虑使用nvarchar文字,它解决了一些类似的数据类型问题:
cast([ntext2] as nvarchar(max)) <> N'1,032.5'
If you would prefer not to cast, you can get by in some scenarios using LIKE
or PATINDEX
, as demonstrated on this MSDN thread: http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/6bd4c661-ea0b-435f-af78-097e61549d41 如果您不想进行强制转换,可以在某些场景中使用
LIKE
或PATINDEX
,如此MSDN线程所示: http : //social.msdn.microsoft.com/Forums/en-US/transactsql/thread/6bd4c661 -ea0b-435f-af78-097e61549d41
The LIKE expression, without wildcards, would be (in this case) roughly equivalent to a test for equality. 没有通配符的LIKE表达式(在这种情况下)大致相当于对相等性的测试。
In this case, the expression would be: 在这种情况下,表达式将是:
[ntext2] NOT LIKE '1,032.5'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.