Search code examples
sql-serversql-server-2008t-sqlsql-server-2014

Operand data type image is invalid for max operator in SQL Server 2014


I have a query that works in dev 2008 but does not work in test 2014. I need the query returned in the exact format due to display requiremnts, but I don't know how to convert it. I tried casting and converting.

The error message i get on 2014 when i run the query is

Msg 8117, Level 16, State 1, Line 3 Operand data type image is invalid for max operator.

Here is the query:

SELECT Max(CASE
             WHEN rn % 3 = 0 THEN itemtransactionbatchid
           END) AS ItemTransactionBatchId,
       Max(CASE
             WHEN rn % 3 = 0 THEN referencebytes
           END) AS ReferenceBytes,
       Max(CASE
             WHEN rn % 3 = 0 THEN description
           END) AS Description,
       Max(CASE
             WHEN rn % 3 = 1 THEN itemtransactionbatchid
           END) AS ItemTransactionBatchId1,
       Max(CASE
             WHEN rn % 3 = 1 THEN referencebytes
           END) AS ReferenceBytes1,
       Max(CASE
             WHEN rn % 3 = 1 THEN description
           END) AS Description1,
       Max(CASE
             WHEN rn % 3 = 2 THEN itemtransactionbatchid
           END) AS ItemTransactionBatchId2,
       Max(CASE
             WHEN rn % 3 = 2 THEN referencebytes
           END) AS ReferenceBytes2,
       Max(CASE
             WHEN rn % 3 = 2 THEN description
           END) AS Description2
FROM  (SELECT a.itemtransactionbatchid,
              a.referencebytes,
              b.description,
              ( ROW_NUMBER()
                  OVER(
                    ORDER BY CASE WHEN b.code='SIGOPRT' THEN 1 WHEN b.code='SIGDRVR' THEN 2 WHEN b.code='SIGTRANS' THEN 3 WHEN b.code='SIGRECV' THEN 4 WHEN b.code='SIGN' THEN 5 WHEN b.code='SIGSUPR' THEN 6 ELSE 0 END, b.code, (SELECT NULL)) - 1 ) rn
       FROM   [Transaction].itemtransactionref a (NOLOCK)
              JOIN [Transaction].transactionreftype b
                ON a.transactionreftypeid = b.transactionreftypeid
       WHERE  b.code IN( 'SIGDRVR', 'SIGN', 'SIGOPRT', 'SIGRECV',
                         'SIGSUPR', 'SIGTRANS' )
              AND a.itemtransactionbatchid = xxxxxxx
              AND referencebytes IS NOT NULL)x
GROUP  BY rn / 3  

Solution

  • Change the datatype of the image column to VARBINARY(MAX); MAX on that type is supported.


    This does not give any errors:

    CREATE TABLE #t(entr VARBINARY(MAX));
    
    SELECT MAX(entr) FROM #t;