开发者

SQL Server: IN ('asd') not working when column is NTEXT

How can I fix this problem?

where someNtext IN ('asd',asd1')

gives an error:

Msg 402, L开发者_如何学JAVAevel 16, State 1, Line XXXXX

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


An IN list is just short-hand for OR conditions. The LIKE clause works with NTEXT and TEXT fields. So, you can combine those two ideas to do this:

WHERE (
       someNtext LIKE N'asd'
OR     someNtext LIKE N'asd1'
      )

However, as @marc_s suggested in a comment on the Question, NVARCHAR(MAX) is preferred as all string functions work with it (and the TEXT, NTEXT, and IMAGE datatypes have been deprecated as of SQL Server 2005). You could do an inline convert such as:

WHERE CONVERT(NVARCHAR(MAX), someNtext) IN (N'asd', N'asd1')

but likely that would not perform as well as using the LIKE clause with OR conditions.

Please note: When working with NTEXT / NVARCHAR / NCHAR / XML data, it is best to always prefix string literals with an uppercase "N". Not doing so can result in data loss for any characters not supported by the code page associated with the default collation of the database.

For more information on working with collations / encodings / Unicode / strings in general in SQL Server, please visit: https://Collations.Info/


From http://msdn.microsoft.com/en-us/library/ms187993.aspx:

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.


NText is not comparable you screwed, don't use it if you need compare use nvarchar(MAX)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜