Articles Comments

Oracle DBA & All IT » Database, Function, Oracle, Oracle Tips, Programming, Tuning Performance » Decode = IF-THEN-ELSE (The Oracle decode function)

Decode = IF-THEN-ELSE (The Oracle decode function)

Decode = IF-THEN-ELSE

decode function สามารถที่จะใช้ใน SQL มีค่าเท่ากับ IF-THEN-ELSE เป็นทางเลือกหนึ่งที่ไม่ต่างจากการใช้ CASE ใน statement

Syntax:
decode( expression , compare_value, return_value, [,compare, return_value] … [,default_return_value] )

 

ความหมาย:
expression คือ ค่าที่จะเอามาใช้เปรียบเทียบเงื่อนไข
compare_value คือ ค่าที่สามารถจะมาเปรียบเทียบได้กับ expresssion
return_value คือค่าที่จะแสดงออกมาเมื่อ ค่าของ expression = compare_value
default_return_value คือค่าที่จะแสดง เมื่อ expression <> compare_value

 

ลองมาดูตัวอย่างกัน :
select id, decode(status,'A','Accepted','D','Denied','Other')
from   contracts;

Statement ด้านบนจะมีค่าเท่ากับการ If ด้านล่าง :
If status = 'A' then Showmessage 'Accepted'
If status = 'D' then Showmessage  'Denied'
Else   Showmessage 'Other'
 

โดยความหมายของด้านบนคือ :
ถ้า column status ของ table contracts เป็น 'A' ก็ให้แสดง คำว่า 'Accepted'
ถ้า column status ของ table contracts เป็น 'D' ก็ให้แสดง คำว่า 'Denied'
ถ้าไม่ตรงกับ 2 ค่าด้านบนเลยให้แสดง คำว่า 'Other'

 

วันนี้เอามาฝากกันสั้นๆ น่าจะพอมีประโยชน์บ้างไม่มากก็น้อยค่ะ ^^

.. Preview : 19565

Related Search:

  • decode sql
  • decode oracle
  • oracle decode
  • decode oracle คือ
  • sql decode
  • decode sql คือ
  • decode คือ
  • SELECT DECODE

Written by

บอกเล่าสิ่งที่พบเจอมาในการทำงาน ประสบการณ์การทำงานด้าน DBA ถ่ายทอดกันด้วยภาษาง่ายๆ บ้านๆ " ทุกอย่างไม่อยาก แต่... แค่ตั้งใจไม่พอ ต้องลงมือทำ และทำ GoodLuck " Fanpage: www.facebook.com/DBAor .. "Oracle Database Consultant " ..

Filed under: Database, Function, Oracle, Oracle Tips, Programming, Tuning Performance · Tags: , , , , , , ,