An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Saturday, February 28, 2009

MySQL IF, MySQL IFNULL - Control Flow Functions

No comments :
Keywords: MySQL, MySQL Control Flow Functions, MySQL IF, MySQL IFNULL, MySQL IF & IFNULL, MySQL Select IF Statements, MySQL Select

MySQL IF - Control Flow Functions

In MySQL, there is a "IF" function allows us deciding the output string in the select statement:- 

Syntax:-

mysql> IF( expr1, expr2, expr3)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. 

Example:-

mysql> select if( user_sex = 'M', 'Male', 'Female') as sex from users;
The above select statement will return 'Male' or 'Female' depends on the column value 'M'or 'F'.

There are some case we may not want "NULL" as output, in that case, use mysql IFNULL( )

Syntax:-

mysql> IFNULL( expr/column, 'Replacing Output String' );
IFNULL()  returns a numeric or string value, depending on the context in which it is used.

Example:-

mysql> select IFNULL( addresscolumn, 'No Address Provided' ) from users;

Keywords: MySQL, MySQL Control Flow Functions, MySQL IF, MySQL IFNULL, MySQL IF & IFNULL, MySQL Select IF Statements, MySQL Select

Featured Blog Topics:


No comments :