![]() dĭ is an expression to return when e does not equal any searched value s1, s2. The r1, r2, …, or rn is the expression to return when e is equal to s. Note that s2, s3, … sn are automatically converted to the data type of s1 before comparing. The s1, s2, … or sn is an expression to search for. The function automatically converts e to the data type of s1 before comparing. The first argument e is the value to be searched. , ) Ĭode language: SQL (Structured Query Language) ( sql ) Arguments e The following illustrates the syntax of the Oracle DECODE() function: DECODE (e, s1, r1. The query works like the following if-then-elsif-else statement: IF 3 = 1 THENĬode language: SQL (Structured Query Language) ( sql ) Oracle DECODE() function syntax The query returned: Not one or two Code language: SQL (Structured Query Language) ( sql ) If you want to specify a default value when the function does not find any match, you do it as follows: SELECT DECODE( 3, 1, 'One', 2, 'Two', 'Not one or two') It works like the following if-then-elsif statement: IF 2 = 1 THENĬode language: SQL (Structured Query Language) ( sql ) If they are equal, the function returns the fifth argument (Two). Otherwise, it compares the first argument with the fourth argument (2). If the first argument equals the second one, the function returns the third argument (One). In this example, the function compares the first argument (2) with the second one. The result is: Two Code language: SQL (Structured Query Language) ( sql ) What if you want to compare the first argument with a list of arguments? See the following example: SELECT DECODE( 2, 1, 'One', 2, 'Two') It works like the following if-then-else statement: IF 1 = 2 THENĮND IF Code language: SQL (Structured Query Language) ( sql ) 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: SELECT DECODE( 1, 2, 'One', 'Not one') The query returns a null value because one does not equal two. The following example is slightly different from the one above. It works like the following if statement IF 1 = 1 THENĮND IF Code language: SQL (Structured Query Language) ( sql ) Because they are equal, the function returns the third argument which is the string 'One': SELECT DECODE( 1, 1, 'One')ĭual Code language: SQL (Structured Query Language) ( sql ) In the following example, the Oracle DECODE() function compares the first argument (1) with the second argument (1). ![]() The Oracle DECODE() function allows you to add the procedural if-then-else logic to the query. ![]() Summary: in this tutorial, you will learn how to use the Oracle DECODE() function to embed if-then-else logic in SQL queries.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |