What's the right way to compare an NTEXT column with a constant value?

后端 未结 2 549
予麋鹿
予麋鹿 2020-12-24 11:17

If I use something like

[ntext2] <> \'1,032.5\',

I get this error:

The data types ntext and varchar are incomp

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 12:04

    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'
    

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

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

提交回复
热议问题