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

試著忘記壹切 提交于 2019-12-06 10:55:37

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)

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