[DB/SQL] conditional function in SQL

최대 1 분 소요

sort of Functions

There are two sort of Functions, Internal Function and User-defined Function. Or, Single Row Function adn Multi Row Function.

conditional function

IF(<statement>, <value if statement is true>, <value if statement is false>)

You can use IF function like this,

SELECT *, IF(<field name>=<certain field name>) AS <alias name> FROM <table name>;

IFNULL and NULLIF

IFNULL(<value1>, <value2>)

if value1 is NULL, it returns value2. if value1 is not NULL, then returns value1.

NULLIF(<value1>, <value2>)

if value1 and value2 are same, it returns NULL, or it returns value1.

CASE … WHEN

CASE <column name>
WHEN <value1> THEN <return1>
WHEN <value2> THEN <return2>
...
ELSE <etc return>
END

you can enter conditional statement in <value>

COALESE

COALESCE (<value1>, <value2>, ... , <valueN>)

if value1 is NULL, return value2.
if value1 and value2 both NULL, return value3
.
.
.
if value1 ~ value(N-1) are NULL, return valueN

The End

태그:

카테고리:

업데이트:

댓글남기기