Distinct error with image field

瘦欲@ 提交于 2019-12-11 23:55:26

问题


I have table which contains a Image column. Now I want to select distinct value of image column, but it gives error. Column name is also Image.

My query is: select Image from tbl1

Error is: The image data type cannot be selected as DISTINCT because it is not comparable.

So how to select distinct value from tb1 table


回答1:


Image data types in a SELECT statement that contains the DISTINCT clause. Depending on the version of SQL Server being used, there are a few ways of overcoming this restriction.

For SQL Server 2000, a TEXT column can be converted to a VARCHAR data type, an NTEXT column can be converted to an NVARCHAR data type while an IMAGE data type can be converted to VARBINARY data type. The SELECT statements earlier which contain the DISTINCT clause can be rewritten as follows and avoid the error message:

SELECT DISTINCT [BookTitle], CAST([BookSummary] AS NVARCHAR(4000)) AS [BookSummary] FROM [dbo].[Book]

SELECT DISTINCT [BookTitle], CAST([BookImage] AS VARBINARY(8000)) AS [BookImage] FROM [dbo].[Book] For SQL Server 2005 and SQL Server 2008 (and later), instead of limiting the NVARCHAR to 4000 characters or the VARCHAR or VARBINARY to 8000 characters, the MAX specifier can be used in its place, as can be seen in the following SELECT statements:

SELECT DISTINCT [BookTitle], CAST([BookSummary] AS NVARCHAR(MAX)) AS [BookSummary] FROM [dbo].[Book]

SELECT DISTINCT [BookTitle], CAST([BookImage] AS VARBINARY(MAX)) AS [BookImage] FROM [dbo].[Book] If using SQL Server 2005 or SQL Server 2008 (or later), another way of overcoming this restriction and this error message without using the CAST or CONVERT function to convert the text, ntext and image data types to varchar, nvarchar and varbinary, respectively, is to change the data types of the columns to VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX). TEXT, NTEXT and IMAGE data types will be removed in a future version of Microsoft SQL Server and use of these data types should be avoided.

Reference: http://www.sql-server-helper.com/error-messages/msg-421.aspx



来源:https://stackoverflow.com/questions/24444841/distinct-error-with-image-field

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