Geeks Talk

Prepare for your Next Interview




Decode in SQL

This is a discussion on Decode in SQL within the Oracle forums, part of the Databases category; What is the purpose or usage of decode function in SQL. Someone brief me on the syntax also. If you could give an example for this function it would help ...


Go Back   Geeks Talk > Databases > Oracle

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-12-2006
Contributing Member
 
Join Date: May 2006
Posts: 84
Thanks: 0
Thanked 9 Times in 6 Posts
timmy is on a distinguished road
Decode in SQL

What is the purpose or usage of decode function in SQL. Someone brief me on the syntax also. If you could give an example for this function it would help me?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-16-2006
Expert Member
 
Join Date: Jun 2006
Location: India
Posts: 411
Thanks: 15
Thanked 33 Times in 25 Posts
jamesravid is on a distinguished road
Re: Decode in SQL

Here is syntax of DECODE function in Oracle

DECODE(expr,search1,result1[,search2,result2]...[,default_val]);

DECODE compares expr to each search(search1,search2...) value one by one. If expr is equal to a search1, then Oracle returns result1 else it compares expr to search2 and so on. If no match is found, then Oracle returns default_val.

the above decode function works exactly like the following if... else if...else statement.

if (expr = search1) then
return result1
else if expr= search2) then
return result2
...
else
return default_val;



Example:

SELECT product_id,
DECODE (id, 1, 'Southlake',
2, 'San Francisco',
3, 'New Jersey',
4, 'Seattle',
'Non-domestic')
Location FROM inventris
WHERE prod_id < 175;


This example decodes the value warehouse_id. If id is 1, then the function returns 'Southlake'; if warehouse_id is 2, then it returns 'San Francisco'; and so forth. If warehouse_id is not 1, 2, 3, or 4, then the function returns 'Non-domestic'.



I hope this helps .
Reply With Quote
  #3 (permalink)  
Old 07-16-2006
Contributing Member
 
Join Date: Jul 2006
Posts: 76
Thanks: 0
Thanked 4 Times in 1 Post
nancyphilips is on a distinguished road
Thumbs up Re: Decode in SQL

Thank you it was a nice explanation with a detailed example. Hope it helped many people in this discussion
Reply With Quote
Reply

  Geeks Talk > Databases > Oracle


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Operators in SQL Allan Paul Oracle 2 06-24-2006 01:50 PM
where to write sql query at runtime dssanjay WinRunner 1 06-03-2006 08:02 AM
What are the New Application Frameworks in SQL Server 2005 admin Testing Issues 1 05-30-2006 07:07 PM
ADO.NET new Features in SQL Server 2005 admin Testing Issues 0 05-29-2006 07:05 PM
Explain about SMO in SQL Server 2005 admin Testing Issues 0 05-29-2006 06:56 PM


All times are GMT -4. The time now is 05:06 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved