Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Versions available for this page: CUBRID 8.3.0 |  CUBRID 8.3.1 |  CUBRID 8.4.0 |  CUBRID 8.4.1 |  CUBRID 8.4.3 |  CUBRID 9.0.0 | 

IF Function

Description

The IF function returns expression2 if the value of the arithmetic expression specified as the first parameter is TRUE, or expression3 if the value is FALSE or NULL. expression2 and expression3 which are returned as a result must be the same or of a convertible common type. If one is explicitly NULL, the result of the function follows the type of the non-NULL parameter.

Syntax

IF( expression1, expression2, expression3 )

 

result:

exrpession2 | expression3

IF(a, b, c) has the same meaning as the CASE statement in the following example:

CASE WHEN a IS TRUE THEN b

ELSE c

END

Example

SELECT * FROM case_tbl;

            a

=============

            1

            2

            3

         NULL

 

--IF function returns the second expression when the fist is TRUE

SELECT a, IF(a=1, 'one', 'other') FROM case_tbl;

            a   if(a=1, 'one', 'other')

===================================

            1  'one'

            2  'other'

            3  'other'

         NULL  'other'

 

--If function in WHERE clause

SELECT * FROM case_tbl WHERE IF(a=1, 1, 2) = 1;

            a

=============

            1