Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Conditional Expression Functions

Updated on 2024-05-07 GMT+08:00

Conditional Expression Functions

  • coalesce(expr1, expr2, ..., exprn)

    Description:

    Returns the first of its arguments that are not null.

    COALESCE(expr1, expr2) is equivalent to CASE WHEN expr1 IS NOT NULL THEN expr1 ELSE expr2 END.

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT coalesce(NULL,'hello');
     coalesce
    ----------
     hello
    (1 row)
    

    Note:

    • Null is returned only if all parameters are null.
    • This value is replaced by the default value when data is displayed.
    • Like a CASE expression, COALESCE only evaluates the parameters that are needed to determine the result. That is, parameters to the right of the first non-NULL parameter are not evaluated.
  • decode(base_expr, compare1, value1, Compare2,value2, ... default)

    Description: Compares base_expr with each compare(n) and returns value(n) if they are matched. If base_expr does not match each compare(n), the default value is returned.

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT decode('A','A',1,'B',2,0);
     case
    ------
     1
    (1 row)
    

    Note: Operations on the XML data are not supported.

  • nullif(expr1, expr2)

    Description: Returns NULL only when expr1 is equal to expr2. Otherwise, expr1 is returned.

    nullif(expr1, expr2) is equivalent to CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END.

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT nullif('hello','world');
     nullif 
    --------
     hello
    (1 row)
    

    Note: Operations on the XML data are not supported.

    Assume the two parameter data types are different:

    • If implicit conversion exists between the two data types, implicitly convert the parameter of lower priority to this data type using the data type of higher priority. If the conversion succeeds, computation is performed. Otherwise, an error is reported. Example:
      1
      2
      3
      4
      5
      gaussdb=# SELECT nullif('1234'::VARCHAR,123::INT4);
       nullif 
      --------
         1234
      (1 row)
      
      1
      2
      gaussdb=# SELECT nullif('1234'::VARCHAR,'2012-12-24'::DATE);
      ERROR:  invalid input syntax for type timestamp: "1234"
      
    • If implicit conversion is not applied between two data types, an error is displayed. Example:
      1
      2
      3
      4
      5
      gaussdb=# SELECT nullif(TRUE::BOOLEAN,'2012-12-24'::DATE);
      ERROR:  operator does not exist: boolean = timestamp without time zone
      LINE 1: SELECT nullif(TRUE::BOOLEAN,'2012-12-24'::DATE) FROM sys_dummy;
      ^
      HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
      
  • nvl( expr1 , expr2 )

    Description:

    • If the value of expr1 is NULL, the value of expr2 is returned.
    • If the value of expr1 is not NULL, the value of expr1 is returned.

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT nvl('hello','world');
      nvl  
    -------
     hello
    (1 row)
    

    Parameters expr1 and expr2 can be of any data type. If expr1 and expr2 are of different data types, NVL checks whether expr2 can be implicitly converted to expr1. If it can, the expr1 data type is returned. If epr2 cannot be implicitly converted to expr1 but epr1 can be implicitly converted to expr2, the expr2 data type is returned. If no implicit type conversion exists between the two parameters and the parameters are different data types, an error is reported.

  • nvl2(expr1, expr2, expr3)

    Description:

    • If expr1 is NULL, expr3 is returned.
    • If expr1 is not NULL, expr2 is returned.
      NOTE:

      This function is valid only when the value of a_format_version is 10c and that of a_format_dev_version is s1 in an ORA-compatible database.

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT nvl2('hello','world','other');
      case  
    -------
     world
    (1 row)
    

    Note: The expr2 and expr3 parameters can be of any type. If the last two parameters of NVL2 are of different types, check whether expr3 can be implicitly converted to expr2. If expr3 cannot be implicitly converted to expr2, an error is returned. If the first parameter is of the numeric type, the function converts this parameter and other parameters to the numeric type, and then compares them. If the parameters cannot be converted, an error message is displayed. If the first parameter is of another type, the function converts other parameters to the type of the first parameter for comparison. If the parameters cannot be converted, an error message is displayed.

  • greatest(expr1 [, ...])

    Description: Selects the largest value from a list of any number of expressions.

    Return type:

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT greatest(1*2,2-3,4-1);
     greatest 
    ----------
            3
    (1 row)
    
    1
    2
    3
    4
    5
    gaussdb=# SELECT greatest('HARRY', 'HARRIOT', 'HAROLD');
     greatest 
    ----------
     HARRY
    (1 row)
    

    Note: Operations on the XML data are not supported.

    NOTE:

    In the scenario where this function is in an ORA-compatible database, the value of a_format_version is 10c, and that of a_format_dev_version is s1:

    1. If the value of any parameter is null, the function returns null.
    2. If the first parameter is of the numeric type, the function converts this parameter and other parameters to the numeric type, and then compares them. If the parameters cannot be converted, an error message is displayed. If the first parameter is of another type, the function converts other parameters to the type of the first parameter for comparison. If the parameters cannot be converted, an error message is displayed.
  • least(expr1 [, ...])

    Description: Selects the smallest value from a list of any number of expressions.

    Example:

    1
    2
    3
    4
    5
    gaussdb=# SELECT least(1*2,2-3,4-1);
     least 
    -------
        -1
    (1 row)
    
    1
    2
    3
    4
    5
    gaussdb=# SELECT least('HARRY','HARRIOT','HAROLD');
     least  
    --------
     HAROLD
    (1 row)
    

    Note: Operations on the XML data are not supported.

    NOTE:

    In the scenario where this function is in an ORA-compatible database, the value of a_format_version is 10c, and that of a_format_dev_version is s1:

    1. If the value of any parameter is null, the function returns null.
    2. If the first parameter is of the numeric type, the function converts this parameter and other parameters to the numeric type, and then compares them. If the parameters cannot be converted, an error message is displayed. If the first parameter is of another type, the function converts other parameters to the type of the first parameter for comparison. If the parameters cannot be converted, an error message is displayed.
  • EMPTY_BLOB()

    Description: Initiates a BLOB variable in an INSERT or an UPDATE statement to a NULL value.

    Return type: BLOB

    Example:

    1
    2
    3
    4
    5
    6
    -- Create a table.
    gaussdb=# CREATE TABLE blob_tb(b blob,id int) DISTRIBUTE BY REPLICATION;
    -- Insert data.
    gaussdb=# INSERT INTO blob_tb VALUES (empty_blob(),1);
    -- Drop the table.
    gaussdb=# DROP TABLE blob_tb;
    

    Note: The length is 0 obtained using DBE_LOB.GET_LENGTH.

  • EMPTY_CLOB()

    Description: Initiates a CLOB variable in an INSERT or UPDATE statement to a null value.

    NOTE:

    This function is valid only when the value of a_format_version is 10c and that of a_format_dev_version is s1 in an ORA-compatible database.

    Return type: CLOB

    Example:

    1
    2
    3
    4
    5
    6
    -- Create a table.
    gaussdb=# CREATE TABLE clob_tb(c clob,id int);
    -- Insert data.
    gaussdb=# INSERT INTO clob_tb VALUES (empty_clob(),1);
    -- Drop the table.
    gaussdb=# DROP TABLE clob_tb;
    

    Note: The length is 0 obtained using DBE_LOB.GET_LENGTH.

  • lnnvl(condition)

    Description: Checks the condition in the WHERE clause of a query statement. If the condition is true, false is returned. If the condition is unknown or false, true is returned.

    condition: The value must be a logical expression. However, it cannot be used in composite conditions with keywords such as AND, OR, and BETWEEN.

    Return type: Boolean

    Example:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    -- Create a table.
    gaussdb=# CREATE TABLE student_demo (name VARCHAR2(20), grade  NUMBER(10,2));
    CREATE TABLE
    
    -- Insert data.
    gaussdb=# INSERT INTO student_demo VALUES ('name0',0);
    INSERT 0 1
    gaussdb=# INSERT INTO student_demo VALUES ('name1',1);
    INSERT 0 1
    gaussdb=# INSERT INTO student_demo VALUES ('name2',2);
    INSERT 0 1
    
    -- Call lnnvl.
    gaussdb=# SELECT * FROM student_demo WHERE LNNVL(name = 'name1');
     name  | grade 
    -------+-------
     name0 |  0.00
     name2 |  2.00
    (2 rows)
    
    NOTE:

    The lnnvl function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2 in an ORA-compatible database.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback