跳到主要内容

Conditional Functions

This page provides a comprehensive overview of Conditional functions in Databend, organized by functionality for easy reference.

Basic Conditional Functions

FunctionDescriptionExample
IF / IFFReturns a value based on a conditionIF(1 > 0, 'yes', 'no')'yes'
CASEEvaluates conditions and returns a matching resultCASE WHEN 1 > 0 THEN 'yes' ELSE 'no' END'yes'
DECODECompares expression to search values and returns resultDECODE(2, 1, 'one', 2, 'two', 'other')'two'
COALESCEReturns the first non-NULL expressionCOALESCE(NULL, 'hello', 'world')'hello'
NULLIFReturns NULL if two expressions are equal, otherwise the first expressionNULLIF(5, 5)NULL
IFNULLReturns the first expression if not NULL, otherwise the secondIFNULL(NULL, 'default')'default'
NVLReturns the first non-NULL expressionNVL(NULL, 'default')'default'
NVL2Returns expr2 if expr1 is not NULL, otherwise expr3NVL2('value', 'not null', 'is null')'not null'

Comparison Functions

FunctionDescriptionExample
GREATESTReturns the largest value from a listGREATEST(1, 5, 3)5
LEASTReturns the smallest value from a listLEAST(1, 5, 3)1
GREATEST_IGNORE_NULLSReturns the largest non-NULL valueGREATEST_IGNORE_NULLS(NULL, 5, 3)5
LEAST_IGNORE_NULLSReturns the smallest non-NULL valueLEAST_IGNORE_NULLS(NULL, 5, 3)3
BETWEENChecks if a value is within a range5 BETWEEN 1 AND 10true
INChecks if a value matches any value in a list5 IN (1, 5, 10)true

NULL and Error Handling Functions

FunctionDescriptionExample
IS_NULLChecks if a value is NULLIS_NULL(NULL)true
IS_NOT_NULLChecks if a value is not NULLIS_NOT_NULL('value')true
IS_DISTINCT_FROMChecks if two values are different, treating NULLs as equalNULL IS DISTINCT FROM 0true
IS_ERRORChecks if an expression evaluation resulted in an errorIS_ERROR(1/0)true
IS_NOT_ERRORChecks if an expression evaluation did not result in an errorIS_NOT_ERROR(1/1)true
ERROR_ORReturns the first expression if it's not an error, otherwise the secondERROR_OR(1/0, 0)0
开始使用 Databend Cloud
低成本
快速分析
多种数据源
弹性扩展
注册