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

末鹿安然 提交于 2019-12-10 11:03:51

问题


How can I fix this problem?

where someNtext IN ('asd',asd1')

gives an error:

Msg 402, Level 16, State 1, Line XXXXX
The data types ntext and varchar are incompatible in the equal to operator.


回答1:


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/




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/6002514/sql-server-in-asd-not-working-when-column-is-ntext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!