Home > Database > SQL > Usage of decode in sql

Usage of decode in sql

下次还敢
Release: 2024-05-02 05:00:31
Original
1096 people have browsed it

The DECODE function is used to evaluate different values ​​based on conditional expressions, similar to the IF-ELSE syntax. Its usage includes: replacing NULL values, converting data types, and returning different values ​​based on conditions.

Usage of decode in sql

Usage of DECODE function in SQL

Overview of DECODE function

## The #DECODE function is a SQL function that evaluates different values ​​based on a conditional expression. It is similar to the IF-ELSE construct, but allows for a more concise syntax.

Syntax

<code class="sql">DECODE(expression, value1, result1, value2, result2, ..., default_result)</code>
Copy after login

Parameters

  • expression: The expression to evaluate.
  • value1, value2, ...: The condition value to be checked.
  • result1, result2, ...: The result value returned when matching the corresponding condition value.
  • default_result: The default result value returned if the expression does not match any condition value.

Usage

DECODE function has the following usage:

  • Replace NULL value:You can use DECODE The function replaces the NULL value with the specified value, for example:
<code class="sql">SELECT DECODE(name, NULL, 'Unknown', name) FROM customers;</code>
Copy after login
  • Convert data type: You can use the DECODE function to convert data from one type to another, For example:
<code class="sql">SELECT DECODE(age, NULL, 0, age) FROM employees;</code>
Copy after login
  • Return different values ​​​​according to conditions: You can use the DECODE function to return different values ​​​​according to conditions, for example:
<code class="sql">SELECT DECODE(gender, 'M', 'Male', 'F', 'Female', 'Other') FROM students;</code>
Copy after login

Example

The following example demonstrates the use of the DECODE function:

<code class="sql">CREATE TABLE sales (
  product_id INT,
  product_name VARCHAR(50),
  category VARCHAR(20),
  sales_amount DECIMAL(10, 2)
);

INSERT INTO sales (product_id, product_name, category, sales_amount) VALUES
(1, 'Smart TV', 'Electronics', 2000.00),
(2, 'iPhone', 'Electronics', 1500.00),
(3, 'Coffee Maker', 'Appliances', 300.00),
(4, 'Microwave', 'Appliances', 450.00);

SELECT
  product_name,
  category,
  DECODE(sales_amount,
    NULL, 'No Sales',
    2000.00, 'High Sales',
    1500.00, 'Medium Sales',
    'Low Sales') AS sales_category
FROM sales;</code>
Copy after login

Output:

<code>product_name | category | sales_category
Smart TV      | Electronics | High Sales
iPhone        | Electronics | Medium Sales
Coffee Maker  | Appliances | Low Sales
Microwave     | Appliances | Low Sales</code>
Copy after login

The above is the detailed content of Usage of decode in sql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template