Search code examples
sqlvarcharcase-when

sql inconsistent datatypes: expected number got char


I am trying to write a Case When statement, but I get an inconsistent datatypes error. I need 'Returned' displayed if the first when statement is not met.

CASE
WHEN (X.RECEIVED_QTY = 0) THEN FLOOR(SYSDATE-INVENTORY_TRANS.TRANSACTION_DATE) 
WHEN (X.RECEIVED_QTY = 0) THEN 'RETURNED'
END AS DAYS_OUT 

Solution

  • You can't mix results of a CASE statement, that means you can't return an INT under one condition and a VARCHAR in another. If you want to return Returned then you will need to CONVERT or CAST your numeric values to VARCHAR

    You also have a syntactical problem with your CASE.

    You'd have to change it to something like this:

    CASE X.RECEIVED_QTY 
      WHEN 0 THEN CAST( FLOOR(SYSDATE-INVENTORY_TRANS.TRANSACTION_DATE) AS VARCHAR(20))
      ELSE 'RETURNED'
    END AS DAYS_OUT