Aaron Thomas Aaron Thomas - 3 months ago 15
SQL Question

Aggregate function with image data type

What is the best way to perform an aggregate function on an

data type?

For example, where binImage is
image
data type, and I want to return either binImage or null:

max( case when somechar = 'A' and anotherchar = 'B' then binImage else null end ) as result


... will not work, because
max
does not support the image data type.

I know that
image
is depreciated.

Answer

Image is really just a varbinary anyway so just cast it as such and then cast it back if you need to. here is an example to show you that it works.

DECLARE @Table AS TABLE (I IMAGE)

INSERT INTO @Table (I) VALUES (CAST('abcdefghi' AS IMAGE)),(CAST('abcdefghi' AS IMAGE)),(CAST('abcdefghi' AS IMAGE))


SELECT CAST(MAX(CAST(I AS VARBINARY(MAX))) AS IMAGE) as Img
FROM
    @Table