The DECODE function in SQL is a conversion function that converts an input expression into a specified value based on given conditions. The syntax is as follows: DECODE(expression, value1, result1, value2, result2, ..., default_result) The DECODE function checks the conditions one by one, finds a match and returns the corresponding result, otherwise it returns the default result. Equivalent to the CASE WHEN statement, but more concise and easier to read.
DECODE function in SQL
Question: What is the DECODE function in SQL?
Answer: The DECODE function is a conversion function that converts an input expression into a specified value, depending on the given conditions.
Detailed description:
The syntax of the DECODE function is as follows:
<code>DECODE(expression, value1, result1, value2, result2, ..., default_result)</code>
Among them:
DECODE function checks the given conditions one by one. When a condition matching expression is found, it returns the corresponding result. If no matching condition is found, it returns default_result.
Example:
Suppose we have a column called "gender" whose value can be "M" (male) or "F" (female). We can use the DECODE function to convert the "gender" value to the following representation:
<code>SELECT DECODE(gender, 'M', 'Male', 'F', 'Female', 'Unknown') AS gender_display FROM table_name;</code>
This query will return the following results:
gender | gender_display |
---|---|
Male | |
Female | |
Unknown |
Note: The DECODE function is equivalent to the CASE WHEN statement. However, the DECODE function is generally more concise and easier to read.
The above is the detailed content of What does decode mean in sql. For more information, please follow other related articles on the PHP Chinese website!