将 NTEXT 列与常量值进行比较的正确方法是什么? [英] What's the right way to compare an NTEXT column with a constant value?

查看:35
本文介绍了将 NTEXT 列与常量值进行比较的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用类似的东西

[ntext2] <> '1,032.5',

我收到此错误:

数据类型 ntext 和 varchar 在不等于运算符中不兼容.

The data types ntext and varchar are incompatible in the not equal to operator.

最好的解决方案是对任何列类型以相同的方式实现比较.(<> 运算符适用于 NVARCHAR 和 INT).

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).

推荐答案

ntext 数据类型已被弃用,取而代之的是 nvarchar(max) 数据类型.如果您可以更改表中的数据类型,那将是最好的解决方案.然后将其与 varchar 文字进行比较就没有问题了.

The ntext data type is deprecated in favour of the nvarchar(max) data type. 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.

否则您必须在比较之前转换该值:

Otherwise you would have to cast the value before comparing it:

cast([ntext2] as nvarchar(max)) <> '1,032.5'

您也可以考虑使用 nvarchar 文字,它解决了一些类似的数据类型问题:

You might also consider using a nvarchar literal, which solves some similar data type problems:

cast([ntext2] as nvarchar(max)) <> N'1,032.5'

这篇关于将 NTEXT 列与常量值进行比较的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆