Thursday, January 14, 2010

Fungsi Logika dalam Query Oracle

Kondisi if selain diletakkan dalam bahasa pemrograman, juga bisa dilakukan didalam sql.
contoh dibawah ini merupakan penggunaan logika if dengan 1 kondisi menggunakan decode dalam oracle :

sintaknya :

decode (expression, search_1, result_1)

Contoh Penggunaan :

"Merubah nominal pada tabel TAGIHAN menjadi negative yang mempunyai KODE_AKUN 1600 "

select KODE_AKUN,JML_TAGIHAN AS JUM_TAGIHAN,
decode(KODE_AKUN, '1600', -(JML_BAYAR), JML_BAYAR) JUM_BAYAR
from TAGIHAN;

Untuk kondisi lebih dari satu bisa menggunakan case when

sintaknya :

case when x = y then a else b end
case when x <>then a when x = y then b else c end

Contoh Penggunaan :

"Merubah nominal pada tabel TAGIHAN menjadi negative yang mempunyai KODE_AKUN 1600 dan 2309 "

select JML_TAGIHAN,
case when
(KODE_AKUN IN ('1600','2309')) then -(JML_BAYAR) else JML_BAYAR end AS JUM_BAYAR
from TAGIHAN;

7 comments:

Anonymous said...

thanks

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

thank you...
I hope you'll post more about oracle