Search code examples
sqlsql-serversql-server-2008varbinarynvarchar

Selecting not null column


I have a table with varbinary(max) column and nvarchar(max) column. One of them is null and the other has a value.

I would like to return the column that has the value as a varbinary(max) column. So far I have tried this, that does not work:

SELECT 
      A =  
      CASE A
         WHEN NULL THEN B
         ELSE A 
      END
FROM Table

Solution

  • SELECT COALESCE(A, CAST(B As varbinary(max)))
    

    UPDATE: In response to comments (thanks) and assuming B is the nvarchar(max) column, I have moved the CAST inside the COALESCE