DECODE () function usage

Source: Internet
Author: User


DECODE () function usage: Compares the input value with the parameter list in the function, and returns 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]); control _ value to be processed. 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. For example: selectdecode (x, 1, 'x is 1', 2, 'x is 2', 'others ') from dual when x is equal to 1, returns 'x is 1 '. If x is equal to 2, 'x is 2' is returned '. 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) www.2cto.com writes all the results in select * from classes t. The data is 1 class 1 class 1 class 1 NUM_12 Class 2 class 2 NUM_23 Class 3 class 3 NUM_34 Class 4 class NUM_4 select t. * from classes t order by decode (t. classnum, 'num _ 1', 4, 'num _ 2', 3, 'num _ 3', 1 ); 1 3 class 3 NUM_32 Class 2 class 2 NUM_23 Class 1 class 1 NUM_14 Class 4 Class 4 NUM_4 all results must be written by myxieyu

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.