简体   繁体   English

将NTEXT列与常量值进行比较的正确方法是什么?

[英]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 如果您不想进行强制转换,可以在某些场景中使用LIKEPATINDEX ,如此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.

相关问题 将ntext列转换为xml并在SQL中获取节点的值 - Convert ntext column to xml and get a node's value in SQL 获取表中列的最大值的最快方法是什么? - What's the fastest way to obtain the maximum value of a column in a table? 查找不在SQL数据库表列中的值的最快方法是什么? - What's the fastest way to find a value that is not in an SQL database table column? 在MySql中,是否有一种方法可以将值仅与列中的数字进行比较? - In MySql is there a way to compare a value to only the digits in a column? 如何从sql中的ntext(以xml格式)列中获取值 - How to get value from ntext (in xml format) column in sql 连接两个表的正确方法是什么,按列分组,select 每条记录只有一行? - what's the right way of joning two tables, group by a column, and select only one row for each record? ntext列中的SQL 2008 SP1 XML值 - SQL 2008 SP1 XML Value from ntext Column 在 Rails 中进行计数的正确方法是什么? - What's the right way to do counts in Rails? 用SQL编写此查询的正确方法是什么? - What's the right way to write this query in SQL? 根据 SQL 中 B 列的值替换 A 列中的值的最佳方法是什么? - What's the best way to replace a value in column A based on the value of column B in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM