Prepare for your Next Interview
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 ...
|
|||
|
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?
|
| Sponsored Links |
|
|||
|
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 . |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |