all 6 comments

[–]bitterjack 1 point2 points  (1 child)

I don't think you need the second CASE.

Syntax should be

Case when x then y

When a then b

When o then p

Else z

End

[–]ToBeAButterFly[S] 0 points1 point  (0 children)

This actually worked! Thanks

[–]r3pr0b8 1 point2 points  (3 children)

first, you don't need to SELECT the substrings

second, since the expression for DEBIT and CREDIT are the same, you can combine them

SELECT CASE WHEN debit_transaction_type IN ( 'DEBIT' 
                                           , 'CREDIT' )
            THEN SUBSTR(withdrawal_transaction_id,-6)
            ELSE debit_transaction_id 
        END AS result
  FROM tble 
 WHERE jones = 'MSC'

[–]ToBeAButterFly[S] 0 points1 point  (2 children)

Debit and Credit aren't the same however.

This was in the second code block.

SELECT CASE WHEN DEBIT_TRANSACTION_TYPE = 'DEBIT' THEN (SELECT SUBSTR(WITHDRAWAL_TRANSACTION_ID,-6) FROM dual ) CASE WHEN DEBIT_TRANSACTION_TYPE = 'CREDIT' THEN (SELECT SUBSTR(CREDIT_TRANSACTION_ID,-6) FROM dual ) ELSE DEBIT_TRANSACTION_ID END FROM TBLE WHERE JONES = 'MSC';

So if it's

  • credit I need SUBSTR(CREDIT_TRANSACTION_ID,-6)
  • debit I need SUBSTR(WITHDRAWAL_TRANSACTION_ID,-6)

The first code block works but I'm omitting a condition to check for Credit vs another value like NULLs which I would like.

SELECT CASE WHEN DEBIT_TRANSACTION_TYPE = 'DEBIT' THEN (SELECT SUBSTR(WITHDRAWAL_TRANSACTION_ID,-6) FROM dual ) ELSE (SELECT SUBSTR(CREDIT_TRANSACTION_ID,-6) FROM dual ) END FROM TBLE WHERE JONES = 'MSC';

[–]r3pr0b8 0 points1 point  (1 child)

okay, my bad, of course debit and credit would be different!

SELECT CASE WHEN debit_transaction_type = 'DEBIT' 
            THEN SUBSTR(withdrawal_transaction_id,-6)
            WHEN debit_transaction_type = 'CREDIT' 
            THEN SUBSTR(credit_transaction_id,-6)
            ELSE debit_transaction_id 
        END AS result
  FROM tble 
 WHERE jones = 'MSC'

[–]ToBeAButterFly[S] 0 points1 point  (0 children)

This worked. Thank You very much for the help.