2

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
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Prajith A S
  • 457
  • 3
  • 8
  • 23
  • Possible duplicate of [MySQL equivalent of DECODE function in Oracle](https://stackoverflow.com/questions/4706100/mysql-equivalent-of-decode-function-in-oracle) – codeforester Dec 19 '18 at 00:26

1 Answers1

4

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
Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Sorry, I am new to mysql. We are using Oracle. in oracle we are using " select sum( decode (txn_type ,'Expense',-txn_amount,txn_amount)as net_amount where id > 0 " to get the result. If possible, please specify what i have to write in mysql to get the result which i have getting in Oracle. – Prajith A S Sep 08 '13 at 05:58
  • I added the MySQL version of your query. – Barmar Sep 08 '13 at 06:34