Purpose: Compare the input value with the parameter list in the function, and return a corresponding value based on the input value. The parameter list of a function is a sequence of several numbers and their corresponding result values. Of course, if it fails to match any real argument sequence, the function also has the default return value. Unlike other SQL functions, the decode function can recognize and operate null values.
Syntax: Decode (control_value, value1, result1 [, value2, result2…] [, Default_result]);
The value of control _ value. The decode function compares the value with a series of even orders to determine the return value. Value1 is a numerical value that forms an ordinal pair. If the input value matches the value, the corresponding result is returned. Corresponding to an empty return value, you can use the keyword null to indicate that result1 is the result value of an ordinal pair. If default_result does not match any value, the default value returned by the function.
example: Select decode (x, 1, 'x is 1', 2, 'x is 2', 'others ') from dual when X is equal to 1 , Returns 'x is 1' . when X is equal to 2 , Returns 'x is 2' . otherwise, others is returned . Yes. You can use it with the sign () function when comparing two values. Select decode (sign (5-6), 1 'is positive',-1, 'is Nagative', 'is zero'), and can also be implemented using case: select case sign (5-6) when 1 then 'is positive' When-1 then 'is Nagative 'else' is zero' endfrom dual. In addition, you can also use decode in order. For example, the table_subject table has a subject_name column. Sort by language, number, and external order. In this case, you can easily use decode to complete the requirements. Select * From table_subject order by deCODE (subject_name, '', 1, 'mat', 2, '', 3)