Prajith A S
Prajith A S

Reputation: 477

Decoding fields in Mysql

I want to get net amount from my mysql data field. I have tried to decode the fields like the below.. But I am getting the Error:

"Lookup Error - MySQL Database Error: Incorrect parameter count in the call to native function 'decode'"

Can anybody help?

Query I have used:

select decode (txn_type ,'Exense',-txn_amount,txn_amount)as net_amount where id > 0

Upvotes: 2

Views: 18123

Answers (1)

Barmar
Barmar

Reputation: 780788

The MySQL DECODE() function is used for decryption, its signature is:

 DECODE(crypt_str,pass_str)

See the documentation. If you want something equivalent to Oracle's DECODE() function, see:

MySQL equivalent of DECODE function in Oracle

Your query can be rewritten as:

SELECT IF(txn_type = 'Expense', -txn_amount, txn_amount) AS net_amount
WHERE id > 0

or:

SELECT CASE txn_type
            WHEN 'Expense' THEN -txn_amount
            ELSE txn_amount
       END AS net_amount

Upvotes: 4

Related Questions