Oracle DECODE Function Explained with Examples
The Oracle DECODE function can be confusing to those who have never used it. It’s a useful function for comparing values. Learn more about this function and some examples in this video.
Table of Contents
Purpose of the Oracle DECODE Function
The purpose of the Oracle DECODE function is to perform an IF-THEN-ELSE function. It’s similar to a CASE statement, but CASE is a statement where DECODE is a function.
It allows you to provide a value, and then evaluate other values against it and show different results. It works similar to an IF statement within other languages.
The syntax of the DECODE function is:
While you’re here, if you want an easy-to-use list of the main features in Oracle SQL, get my SQL Cheat Sheet here:
DECODE Function Parameters
The parameters of the Oracle DECODE function are:
- expression (mandatory): This is the value to compare.
- search (mandatory): This is the value to compare against the expression .
- result (mandatory): This is what is returned if the search value mentioned previously matches the expression value. There can be multiple combinations of search and result values, and the result value is attached to the previous search value.
- default (optional): If none of the search values match, then this value is returned. If the default value is not provided, the DECODE function will return NULL if no matches are found.
If you compare this to an IF-THEN-ELSE statement, it would look like this:
These arguments can be of any numeric type (NUMBER, BINARY_FLOAT, BINARY DOUBLE) or character types.
If both expression and search are character types, then a character comparison is used and the returned value is a VARCHAR2 data type.
If the provided values are numeric , then Oracle determines the datatype to be returned by checking all of the other data types.
The Oracle DECODE function also uses a feature called “ short-circuit evaluation “, which means that the search values are evaluated only before comparing them to the expression value , rather than evaluating all search values before comparing any of them to the expression. This means that Oracle never evaluates a search if a previous search is equal to an expression.
Oracle DECODE vs CASE
On another page, I go into detail on the Oracle CASE statement . It also allows for IF-THEN-ELSE functionality, similar to the DECODE function.
So, what’s the difference between the Oracle DECODE function and CASE statement?
There are a few differences:
- DECODE is an older function . CASE was introduced with version 8, and DECODE was around before then. CASE was introduced as a replacement for DECODE.
- CASE offers more flexibility than DECODE. Tasks that are hard using DECODE are easy using CASE. This means it is likely to be easier for future developers to work with.
- CASE is easier to read . Even with the formatting of SQL queries, a DECODE statement can be harder to read.
- The way they handle NULL values is different. DECODE treats NULL equal to NULL. CASE treats NULL as not equal to NULL.
When it comes to the performance of both of these functions, there is minimal difference . Some examples show that unless you’re doing iterations over millions of records, you won’t get much of a difference, and even then it will be small and depend on the CPU used. So, the performance should not be a determining factor when deciding whether to use an Oracle CASE statement or DECODE function.
Oracle DECODE Function with NULL Values
As I mentioned above, the Oracle DECODE function is an exception to other functions in how it handles NULL values. It treats a NULL expression and NULL search as equal (so NULL == NULL).
So, this example will return 1:
The CASE statement treats NULL values as not equal, so this is an important distinction when working with this function.
Can You Use The Oracle DECODE Function In The WHERE Clause?
Yes, you can use DECODE in the WHERE clause. It’s done in the same way as any other checks.
See the examples section below for an example on how to use the DECODE function in the WHERE clause.
Can You Use The Oracle DECODE Function With LIKE?
The DECODE function is used to find exact matches. This is one of the drawbacks when comparing it to the CASE statement, as the CASE statement can perform more advanced checks.
However, it is possible to use the Oracle DECODE function with LIKE.
You can do this with a combination of the SIGN function and the INSTR function .
- INSTR will check if a string is contained within another string
- SIGN will check if the INSTR is positive or negative
You can then check the SIGN and INSTR against a value of 1 to see if the match is found. See the examples section for more information on how to do this.
Can You Use The Oracle DECODE Function With Greater Than?
Yes, you can use the DECODE function with greater than, but it’s not neat. Just like using it with a LIKE comparison, it’s better to do with a CASE statement, but still possible with a DECODE.
This can be done using subtraction and a SIGN function.
For example, to check if a value is greater than 1000:
- Use 1000 – value to get a result, which will be positive if the value is less than 1000, and negative if the value is greater than 1000.
- Add this into a SIGN function, which returns -1 for negative and 1 for positive.
- Compare the result to -1, and this will give you the greater than check using DECODE.
Once again, see the example section below for more information on how to do this.
Can You Use The Oracle DECODE Function In an UPDATE Statement?
Yes, you can use the DECODE function in UPDATE statements. See the example section below for more information.
What Is The Oracle DECODE Function Performance?
As mentioned earlier in this article, the performance of CASE vs DECODE is pretty similar. It shouldn’t be used as a determining factor when deciding which method to use.
As a general rule, I would recommend using a CASE statement as it’s easier to read and has more advanced logic.
Examples of the DECODE Function
Here are some examples of the DECODE function. I find that examples are the best way for me to learn about code, even with the explanation above.
This is an example of the DECODE function with one character search.
You can see that when the country = ‘USA’, the DECODE_RESULT is ‘North America’, otherwise it shows NULL.
This is an example of the DECODE function with two character searches.
Similar to Example 1, this shows different results for USA and UK.
This example shows the DECODE function with a default value.
This is the same query as above, but with a default value. You can see that it shows Other instead of NULL.
This is a DECODE function with many character searches.
This shows many different results from the DECODE function, as well as the Other for anything that was not considered.
This query performs a DECODE on number values.
As there is only one check being done, there is only one matching result in this table.
This uses the Oracle DECODE function with three number searches.
Notice that only exact matches are found, not a range or a greater than.
This example uses the DECODE function with NULL to see how it works.
You can see that the record with NULL for a country is shown as No Country, and anything that does not match is shown as NULL.
This is an example of using Oracle DECODE in a WHERE clause.
This only shows records where the DECODE function returns ‘North America’.
This example uses DECODE to find if a value is contained in another value, similar to the LIKE function.
This checks for the value of U and displays a message if it finds it or not.
This is an example of DECODE with greater than functionality.
As you can see, any record where employees is greater than 20 will show one value, and less than will show another value.
This is an example of using an UPDATE statement with DECODE.
As you can see, those records where country is USA have had their last name updated to be ‘American’.
Similar Functions
Some functions which are similar to the Oracle DECODE function are:
- CASE – A statement that was introduced to replace DECODE. Offers more functionality and easier readability compared to DECODE.
You can find a full list of Oracle functions here .
17 thoughts on “Oracle DECODE Function Explained with Examples”
how to convert the numbers to characters like 1 to ‘positive’ and -1 to ‘negative’ using functions.
Hi Ravi, you could use the DECODE function in this article. However I recommend using the CASE function to check number and convert to ‘positive’ if 1 or ‘negative’ if -1. You can use the SIGN function to determine if a number is positive or negative, or use greater than or less than.
Sir, please send the systax.
Try this: SELECT CASE WHEN numvalue 0 THEN ‘positive’ ELSE ‘even’ END AS numtest FROM yourtable;
Thank you sir.
Hi sir, I have one table like tester2 val1 val2 val3 A B 10 C D 20 E A 25 B A 10 A E 25 D C 20 E F 25 and output is given below val1 val2 val3 A B 10 C D 20 A E 25 E F 25 we can use the functions or joins. So,please send the syntax and which function used in this problem.
Hi Ravi, try this
SELECT DISTINCT(LEAST(VAL1,VAL2)), GREATEST(VAL1,VAL2) , VAL3 FROM TESTER ORDER BY VAL3;
Hi Sir, I have string like x12abde#_13-89. I want to print out the only ‘xabde’ by using functions so, give an syntax for this problem
I am having the another table in which decode values there ,how i can join the tables base on decode value
2. Write SQL query to display employees salary and the following condition using whether CASE/DECODE. If Salary is greater than or equal to 1000 and salary is less than 2000 then add 5 to salary If Salary is greater than or equal to 3500 and salary is less than 4000 then add 10 to salary If Salary is greater than or equal to 4000 and salary is less than 5000 then add 20 to salary
Hi Malak, as mentioned in my other comment, what have you written already?
pls help me to solve this
Write SQL query to display employees salary and the following condition using whether CASE/DECODE. If Salary is greater than or equal to 1000 and salary is less than 2000 then add 5 to salary If Salary is greater than or equal to 3500 and salary is less than 4000 then add 10 to salary If Salary is greater than or equal to 4000 and salary is less than 5000 then add 20 to salary
Hi Malak, what have you written already? Could you use the DECODE function from this article to write a query to do this?
Can you explain me how to get numbers,small and big charecters in different row from single row.
table have data like test 1 2 A B a b c 3 4
i want output like this
num small_chr big_chr 1 a A 2 b B 3 c 4
Hi, I don’t think you can do that in SQL. Each row is a set of data or a record, and having separate columns for different combinations of rows isn’t something SQL can do. Why would you need to do this?
Hi, in Oracle we use decode with partition by to flatfile data onto one line of output.
for instance, select id_number, decode(max(ct,1,degree)degree1, decode(max(ct,1,school)school1, decode(max(ct,1,grad_year) grad_year1, decode(max(ct,2,degree)degree2, decode(max(ct,2,school)school2 decode(max(ct,2,grad_year)grad_year2 from (select row_number() over (partition by education.id_number order by education.graduationdate desc) ct, degree, school, grad_year from education group by degree, school, grad_year)
This makes a quick and elegant way to insert data into a subselect without having to create 2 or sometimes 5 or 6 CTEs (we have grads with that many degrees) but every way I’ve attempted to do this in ms sql server leaves me with one row for each degree, usually with the data in a seperate column for each ct set. is there a way to make a case statement create output that looks like this:
ID_NUMBER| DEGREE1 | SCHOOL1 | GRAD_YEAR1 | DEGREE2 | SCHOOL2 | GRAD_YEAR2 200001 BS SAS 1984 MS SAS 1987
Leave a Comment Cancel Reply
Your email address will not be published. Required fields are marked *
This site uses Akismet to reduce spam. Learn how your comment data is processed .
Advertisement
- Oracle / PLSQL
- Web Development
- Color Picker
- Programming
- Techie Humor
Oracle Basics
- AND & OR
- COMPARISON OPERATORS
- IS NOT NULL
- REGEXP_LIKE
Oracle Advanced
- Alter Table
- Alter Tablespace
- Change Password
- Check Constraints
- Comments in SQL
- Create Schema
- Create Schema Statement
- Create Table
- Create Table As
- Create Tablespace
- Create User
- Declare Variables
- Drop Tablespace
- Error Messages
- Find Default Tablespace
- Find Users Logged In
- Find Version Information
- Global Temporary
- Grant/Revoke Privileges
- Local Temporary
- Primary Keys
- Set Default Tablespace
- System Tables
- Unique Constraints
Oracle Cursors
- Close Cursor
- Cursor Attributes
- Declare Cursor
- Fetch Cursor
- Open Cursor
- Select For Update
- Where Current Of
Oracle Exception Handling
- Named Programmer-Defined Exception
- Named System Exception
- WHEN OTHERS Clause
Oracle Foreign Keys
- Disable Foreign Key
- Drop Foreign Key
- Enable Foreign Key
- Foreign Key
- Foreign Key (cascade delete)
- Foreign Key (set null delete)
Oracle Loops/Conditionals
- CURSOR FOR LOOP
- IF-THEN-ELSE
- REPEAT UNTIL LOOP
Oracle Transactions
- Commit Transaction
- Rollback Transaction
- Set Transaction
Oracle Triggers
- After Delete Trigger
- After Insert Trigger
- After Update Trigger
- Before Delete Trigger
- Before Insert Trigger
- Before Update Trigger
- Disable All Triggers
- Disable Trigger
- Drop Trigger
- Enable All Triggers
- Enable Trigger
String/Char Functions
- Concat with ||
- REGEXP_INSTR
- REGEXP_REPLACE
- REGEXP_SUBSTR
Numeric/Math Functions
- REGEXP_COUNT
- ROUND (numbers)
- TRUNC (numbers)
Date/Time Functions
- CURRENT_DATE
- CURRENT_TIMESTAMP
- LOCALTIMESTAMP
- MONTHS_BETWEEN
- ROUND (dates)
- SESSIONTIMEZONE
- SYSTIMESTAMP
- TRUNC (dates)
Conversion Functions
- CHARTOROWID
- NUMTODSINTERVAL
- NUMTOYMINTERVAL
- TO_DSINTERVAL
- TO_MULTI_BYTE
- TO_SINGLE_BYTE
- TO_TIMESTAMP
- TO_TIMESTAMP_TZ
- TO_YMINTERVAL
Analytic Functions
- FIRST_VALUE
Advanced Functions
- CARDINALITY
- SYS_CONTEXT
Oracle / PLSQL: DECODE Function
This Oracle tutorial explains how to use the Oracle/PLSQL DECODE function with syntax and examples.
Description
The Oracle/PLSQL DECODE function has the functionality of an IF-THEN-ELSE statement.
The syntax for the DECODE function in Oracle/PLSQL is:
Parameters or Arguments
The DECODE function returns a value that is the same datatype as the first result in the list. If the first result is NULL, then the return value is converted to VARCHAR2. If the first result has a datatype of CHAR, then the return value is converted to VARCHAR2. If no matches are found, the default value is returned. If default is omitted and no matches are found, then NULL is returned.
The DECODE function can be used in the following versions of Oracle/PLSQL:
- Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i
The DECODE function can be used in Oracle/PLSQL.
You could use the DECODE function in a SQL statement as follows:
The above DECODE statement is equivalent to the following IF-THEN-ELSE statement:
The DECODE function will compare each supplier_id value, one by one.
Frequently Asked Questions
Question: One of our viewers wanted to know how to use the DECODE function to compare two dates (ie: date1 and date2), where if date1 > date2, the DECODE function should return date2. Otherwise, the DECODE function should return date1.
Answer: To accomplish this, use the DECODE function as follows:
The formula below would equal 0, if date1 is greater than date2:
Helpful Tip #1 : One of our viewers suggested combining the SIGN function with the DECODE function as follows:
The date example above could be modified as follows:
The SIGN/DECODE combination is also helpful for numeric comparisons e.g. Sales Bonuses
Helpful Tip #2 : One of our viewers suggested using the LEAST function (instead of the DECODE function) as follows:
Question: I would like to know if it's possible to use the DECODE function for ranges of numbers, ie 1-10 = 'category 1', 11-20 = 'category 2', rather than having to individually decode each number.
Answer: Unfortunately, you can not use the DECODE function for ranges of numbers. However, you can try to create a formula that will evaluate to one number for a given range, and another number for the next range, and so on.
For example:
In this example, based on the formula:
The formula will evaluate to 0, if the supplier_id is between 1 and 10. The formula will evaluate to 1, if the supplier_id is between 11 and 20. The formula will evaluate to 2, if the supplier_id is between 21 and 30.
and so on...
Question: I need to write a DECODE statement that will return the following:
If yrs_of_service < 1 then return 0.04 If yrs_of_service >= 1 and < 5 then return 0.04 If yrs_of_service > 5 then return 0.06
How can I do this?
Answer: You will need to create a formula that will evaluate to a single number for each one of your ranges.
Question: Is there a limit to the number of arguments that you can have in one DECODE statement? I'm getting an error, "ORA-00939: too many arguments for function".
Answer: Yes, the maximum number of components that you can have in a DECODE function is 255. This includes the expression , search , and result arguments.
Home | About Us | Contact Us | Testimonials | Donate
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy .
Copyright © 2003-2024 TechOnTheNet.com. All rights reserved.
Home » Oracle Comparison Functions » Oracle DECODE Function
Oracle DECODE Function
Summary : in this tutorial, you will learn how to use the Oracle DECODE() function to embed if-then-else logic in SQL queries.
Introduction to Oracle DECODE() function
The Oracle DECODE() function allows you to add the procedural if-then-else logic to the query.
In the following example, the Oracle DECODE() function compares the first argument (1) with the second argument (1). Because they are equal, the function returns the third argument which is the string 'One' :
It works like the following if statement
The following example is slightly different from the one above. The query returns a null value because one does not equal two.
If you want to specify a default value when the first argument is not equal to the second one, you append the default value to the argument list as shown below:
It works like the following if-then-else statement:
What if you want to compare the first argument with a list of arguments? See the following example:
The result is:
In this example, the function compares the first argument (2) with the second one. If the first argument equals the second one, the function returns the third argument (One). Otherwise, it compares the first argument with the fourth argument (2). If they are equal, the function returns the fifth argument (Two).
It works like the following if-then-elsif statement:
If you want to specify a default value when the function does not find any match, you do it as follows:
The query returned:
The query works like the following if-then-elsif-else statement:
Oracle DECODE() function syntax
The following illustrates the syntax of the Oracle DECODE() function:
The first argument e is the value to be searched. The function automatically converts e to the data type of s1 before comparing.
s1, s2, .. sn
The s1, s2, … or sn is an expression to search for. Note that s2, s3, … sn are automatically converted to the data type of s1 before comparing.
r1, r2, .. rn
The r1, r2, …, or rn is the expression to return when e is equal to s.
d is an expression to return when e does not equal any searched value s1, s2, .. sn.
Return value
The DECODE() function returns a value with the data type of the first result (r1, r2, .. rn or d) argument.
You can use expressions for the search (s), the result (r), and the default value (d) in the DECODE() function.
The DECODE() function evaluates each search value (s1, s2, .., or sn) only before comparing it to the first argument (e), rather than evaluating all search values. In other words, the DECODE() function never evaluates a search (si+1) when a previous search (si) equals e.
Oracle DECODE() function examples
Let’s take some examples of using the DECODE() function to see how it works.
A) Use DECODE() function to make data more meaningful
See the following locations table in the sample database :
The following statements return the country that has at least two locations stored in the database:
The country id is quite cryptic. You can use the DECODE() function to make the country data more meaningful as follows:
B) Oracle DECODE with ORDER BY example
Consider the following employees table:
The following query uses the DECODE() function in the ORDER BY clause to sort the employee’s result set based on an input argument:
In this example, we sorted the employee list by job title because we passed the character J as the first argument of the DECODE() function.
Oracle DECODE with GROUP BY example
See the following products table:
The following statement illustrates how to use the DECODE() function in the GROUP BY clause. It returns the number of products whose list prices are higher than, equal to, and less than the average list price.
Oracle DECODE() function with SUM() example
We will use the products and product_categories tables in this example for the demonstration.
The following example uses the DECODE() function to change the ranges to the binary number and uses the SUM() function to count the number of values with a specified range:
Here is the output:
Oracle DECODE() function and NULL
NULL cannot be compared to anything even NULL. However, DECODE() function treats two null values are being equal.
The following statement returns the string Equal:
In this tutorial, you have learned how to use the Oracle DECODE() function to add procedure if-then-else logic to SQL queries.
- Site Feedback
How can I have a decode statement that returns multiple values (e.g. a collection)?
Question and answer.
Thanks for the question, Vinay.
Asked: October 22, 2007 - 2:07 pm UTC
Last updated: October 23, 2007 - 1:27 pm UTC
Version: 10.2.0
and Tom said...
Csv -> table discussed in tom's blog, etc.
Duke Ganote, October 23, 2007 - 11:31 am UTC
Vinay Chandrakant, October 23, 2007 - 2:01 pm UTC
More to Explore
Pl/sql demos.
Check out more PL/SQL tutorials on our LiveSQL tool.
PL/SQL docs
PL/SQL reference manual from the Oracle documentation library
DECODE compares expr to each search value one by one. If expr is equal to a search , then Oracle Database returns the corresponding result . If no match is found, then Oracle returns default . If default is omitted, then Oracle returns null.
The arguments can be any of the numeric types ( NUMBER , BINARY_FLOAT , or BINARY_DOUBLE ) or character types.
If expr and search are character data, then Oracle compares them using nonpadded comparison semantics. expr , search , and result can be any of the datatypes CHAR , VARCHAR2 , NCHAR , or NVARCHAR2 . The string returned is of VARCHAR2 datatype and is in the same character set as the first result parameter.
If the first search-result pair are numeric, then Oracle compares all search-result expressions and the first expr to determine the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.
The search , result , and default values can be derived from expressions. Oracle Database uses short-circuit evaluation . That is, the database evaluates each search value only before comparing it to expr , rather than evaluating all search values before comparing any of them with expr . Consequently, Oracle never evaluates a search if a previous search is equal to expr .
Oracle automatically converts expr and each search value to the datatype of the first search value before comparing. Oracle automatically converts the return value to the same datatype as the first result . If the first result has the datatype CHAR or if the first result is null, then Oracle converts the return value to the datatype VARCHAR2 .
In a DECODE function, Oracle considers two nulls to be equivalent. If expr is null, then Oracle returns the result of the first search that is also null.
The maximum number of components in the DECODE function, including expr , searches , results , and default , is 255.
This example decodes the value warehouse_id . If warehouse_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 '.
Scripting on this page enhances content navigation, but does not change the content in any way.
- Oracle SQL Tuning
- Tuning Oracle SQL
- Extension ANSI SQL
- SQL Tuning Tool Set
- Executing Oracle SQL
- Explain Plan Utility
- SQL Optimizer Modes
- Rule Based Optimizer
- Ranking Scheme
- Cost Based Optimizer
- SQL Tuning Hints
- ANSI SQL Extensions
- Built in Functions
- Query Override
- Oracle null Value BIF
- Decode Function
- Supplied Extensions
- Extensions Conclusion
- SQL Tuning Tools
- DBA SQL Roles
- Creating SQL Standards
- Developers Write SQL
- Inside Stored Procedures
- SQL Declarative Language
- Oracle Plan Table
- Explain Plan Statement
- Full Table Scan
- Diagnose Table Joins
- Non Correlated Subqueries
- Index Range Scans
- Analyze Explain Plan
- SQL Explain Plan Conclusion
- Optimizer Modes
- Default Optimizer Mode
- RelationalDB
- Oracle Extensions
Oracle DECODE built-in function
Decode with grouping and rollup.
- GROUPING tells Oracle to summarize. In this case we have extracted region, job title, and salary information and summarized it by region
- We have rolled up the totals by region_name and by job_title.
- AVG provides a nationwide average for the number of employees and the average salary.
- If this were written without BIFs, it would have become a cumbersome and challenging programming task.
Frequently used Oracle SQL BIFs
- to_char: The to_char function is especially useful for translating DATE datatypes and converting numeric columns to character representations.
- upper: The upper function is often used in queries that search text columns and ensure that retrievals of case-sensitive data are properly serviced.
- lower: The lower function is used to convert text to a lowercase representation and is quite useful when searching for strings in text.
- substr: The substr function is used to extract sub-strings in a large character column. This is commonly used to extract subsets from large character datatype columns such as subsets of telephone numbers.
- decode: The decode function is used to translate values in an SQL statement from a cryptic abbreviation to a readable value. For example, the decode function can translate two-digit State names into the full name of the State.
- GROUPING tells Oracle to summarize. In this case we have extracted region, job title, and salary information and summarized it by region.
Oracle Decode - Exercise
- DECODE function in Oracle
DECODE is an advanced function that the Oracle database supports. It is used to work as an IF-THEN-ELSE statement. The DECODE function is supported in the various versions of the Oracle/PLSQL, including, Oracle 12c, Oracle 11g, Oracle 10g and Oracle 9i.
Parameters: expression: It is used to specify the value to be compared. search: It is used to specify the value to be compared against expression. result: It is used to specify the value to return, if expression is equal to search. default: It is used to specify the default value to be returned if no matches are found.
SELECT name, DECODE ( student_id, 1, 'Tom', 2, 'Mike', 3, 'Harry', 'Jim') result FROM students;
Explanation: Here, each student_id value will be compared one by one by the DEFAULT function. The default value ( ‘Jim’ ) will be returned if no matches are found.
- ORACLE Introduction
- CREATE TABLE in Oracle
- SQLERRM function in Oracle
- SQLCODE function in Oracle
- USERENV function in Oracle
- ACOS function in Oracle
- ADD_MONTHS function in Oracle
- ALTER TABLE in Oracle
- ANTI JOIN in Oracle
- ASCII function in Oracle
- ASCIISTR function in Oracle
- ASIN function in Oracle
- ATAN function in Oracle
- ATAN2 function in Oracle
- AVG function in Oracle
- BFILENAME function in Oracle
- ABS function in Oracle
- BIN_TO_NUM function in Oracle
- BITAND function in Oracle
- CARDINALITY function in Oracle
- CASE function in Oracle
- CAST function in Oracle
- CEIL function in Oracle
- CHARTOROWID function in Oracle
- CHR function in Oracle
- COALESCE function in Oracle
- COMPOSE function in Oracle
- CONCAT with function in Oracle
- CONCAT function in Oracle
- CONVERT function in Oracle
- CORR function in Oracle
- COS function in Oracle
- COUNT function in Oracle
- COVAR_POP function in Oracle
- COVAR_SAMP function in Oracle
- CREATE TABLE AS in Oracle
- VIEW in Oracle
- CROSS JOIN in Oracle
- CUME_DIST function in Oracle
- CURRENT_DATE in Oracle
- CURRENT_TIMESTAMP function in Oracle
- DBTIMEZONE function in Oracle
- DECOMPOSE function in Oracle
- DENSE_RANK function in Oracle
- DROP TABLE in Oracle
- DUMP function in Oracle
- EMPTY_BLOB function in Oracle
- EMPTY_CLOB function in Oracle
- EQUI JOIN in Oracle
- EXP function in Oracle
- EXTRACT function in Oracle
- FIRST_VALUE function in Oracle
- FLOOR function in Oracle
- FROM_TZ function in Oracle
- GLOBAL TEMP TABLES in Oracle
- GREATEST function in Oracle
- GROUP_ID function in Oracle
- HEXTORAW function in Oracle
- INITCAP function in Oracle
- INNER JOIN in Oracle
- INSTR function in Oracle
- INSTR2 function in Oracle
- VARIANCE function in Oracle
- VAR_SAMP function in Oracle
- VAR_POP function in Oracle
- USER function in Oracle
- UID function in Oracle
- TZ_OFFSET function in Oracle
- TRUNCATE TABLE in Oracle
- TRUNC (numbers) function in Oracle
- TRUNC (dates) function in Oracle
- TO_YMINTERVAL function in Oracle
- TO_TIMESTAMP_TZ function in Oracle
- TO_TIMESTAMP function in Oracle
- TO_SINGLE_BYTE function in Oracle
- TO_NUMBER function in Oracle
- TO_NCLOB function in Oracle
- TO_MULTI_BYTE function in Oracle
- TO_LOB function in Oracle
- TO_DSINTERVAL function in Oracle
- TO_DATE function in Oracle
- TO_CLOB function in Oracle
- TO_CHAR function in Oracle
- TANH function in Oracle
- TAN function in Oracle
- SYSTIMESTAMP function in Oracle
- SYSDATE function in Oracle
- SYS_CONTEXT function in Oracle
- SQRT function in Oracle
- SESSIONTIMEZONE function in Oracle
- LOG function in Oracle
- LAST_DAY function in Oracle
- LEAST function in Oracle
- LENGTH function in Oracle
- LENGTH2 function in Oracle
- INSTR4 function in Oracle
- INSTRB function in Oracle
- INSTRC function in Oracle
- LAG function in Oracle
- LAST_VALUE function in Oracle
- LEAD function in Oracle
- LENGTH4 function in Oracle
- LENGTHB function in Oracle
- LENGTHC function in Oracle
- LISTAGG function in Oracle
- LN function in Oracle
- LNNVL function in Oracle
- LOCAL TEMP TABLES in Oracle
- LOCALTIMESTAMP function in Oracle
- MAX function in Oracle
- MEDIAN function in Oracle
- MIN function in Oracle
- MOD function in Oracle
- MONTHS_BETWEEN function in Oracle
- NANVL function in Oracle
- NEW_TIME function in Oracle
- NEXT_DAY function in Oracle
- NULLIF function in Oracle
- NUMTODSINTERVAL function in Oracle
- NUMTOYMINTERVAL function in Oracle
- NVL function in Oracle
- NVL2 function in Oracle
- POWER function in Oracle
- STDDEV function in Oracle
- NTH_VALUE function in Oracle
- SINH function in Oracle
- SIN function in Oracle
- SIGN function in Oracle
- SEMI JOIN in Oracle
- ROUND (numbers) function in Oracle
- AFTER TRIGGER in Oracle
- BEFORE TRIGGER in Oracle
- CURSOR in Oracle
- DELETE query in Oracle
- DISABLE TRIGGER in Oracle
- DISTINCT clause in Oracle
- DROP TRIGGER in Oracle
- ENABLE TRIGGER in Oracle
- FROM clause in Oracle
- FUNCTION in Oracle
- SUM function in Oracle
- SELF JOIN in Oracle
- ROUND (dates) function in Oracle
- REMAINDER function in Oracle
- JOINS in Oracle
- RANK function in Oracle
- RAWTOHEX function in Oracle
- TRIGGER in Oracle
- COSH function in Oracle
- HAVING clause in Oracle
- INSERT ALL in Oracle
- INSERT query in Oracle
- GROUP BY clause in Oracle
- MINUS in Oracle
- ORDER BY clause in Oracle
- PROCEDURE in Oracle
- INTERSECT operator in Oracle
- QUERIES in Oracle
- SELECT query in Oracle
- UNION ALL in Oracle
- UPDATE Query in Oracle
- UNION in Oracle
- OUTER JOIN in Oracle
- REGEXP_COUNT function in Oracle
- ROWNUM in Oracle
IMAGES
COMMENTS
Oracle DECODE Function with NULL Values. As I mentioned above, the Oracle DECODE function is an exception to other functions in how it handles NULL values. It treats a NULL expression and NULL search as equal (so NULL == NULL). So, this example will return 1: SELECT DECODE(NULL, NULL, 1, 0) FROM DUAL; The CASE statement treats NULL values as ...
DECODE compares expr to each search value one by one. If expr is equal to a search, then Oracle Database returns the corresponding result.If no match is found, then Oracle returns default.If default is omitted, then Oracle returns null.. The arguments can be any of the numeric types (NUMBER, BINARY_FLOAT, or BINARY_DOUBLE) or character types.If expr and search are character data, then Oracle ...
Returns. The DECODE function returns a value that is the same datatype as the first result in the list. If the first result is NULL, then the return value is converted to VARCHAR2. If the first result has a datatype of CHAR, then the return value is converted to VARCHAR2. If no matches are found, the default value is returned. If default is omitted and no matches are found, then NULL is returned.
In other words, the DECODE() function never evaluates a search (si+1) when a previous search (si) equals e. Oracle DECODE() function examples. Let's take some examples of using the DECODE() function to see how it works. A) Use DECODE() function to make data more meaningful. See the following locations table in the sample database:
decode is the function Oracle originally provided with releases of the database prior to 8.1.6. Starting with 816, CASE is the standard way to achieve the same results - with more meaning (case is easier to read and understand) and is the recommended function to use. ... Which means if two variables are compared in decode for = then it returns ...
How can I have a decode statement that returns multiple values (e.g. a collection)? Hi Tom, I'd like to use a decode statement that returns multiple values instead of a single value. The statement where I will be using this is something like:select * from v_viewname v where v.enterprise in decode(v_in_enterprise, 'ALL', v.enterprise, TABLE(f_stringto
DECODE . Syntax. Description of the illustration decode.gif. Purpose. DECODE compares expr to each search value one by one. If expr is equal to a search, then Oracle Database returns the corresponding result.If no match is found, then Oracle returns default.If default is omitted, then Oracle returns null.. The arguments can be any of the numeric types (NUMBER, BINARY_FLOAT, or BINARY_DOUBLE ...
The DECODE function within Oracle becomes even more powerful when it is combined with other built-in functions. The diagram below shows what can happen when you use the 1) GROUPING and 2) ROLLUP BIFs with DECODE. GROUPING tells Oracle to summarize. In this case we have extracted region, job title, and salary information and summarized it by region
DECODE is an advanced function that the Oracle database supports. It is used to work as an IF-THEN-ELSE statement. The DECODE function is supported in the various versions of the Oracle/PLSQL, including, Oracle 12c, Oracle 11g, Oracle 10g and Oracle 9i.
The Oracle decode function The decode function can be used in SQL for and IF-THEN-ELSE construction. It's an alternative for the CASE statement which was introduced in Oracle 8. Syntax: decode( expression , compare_value, return_value, [,compare, return_value] ... [,default_return_value] ) with: expression is the value to evaluate