Collections:
IF() - Conditional Value Selection
How to conditionally select a value using the IF() function?
✍: FYIcenter.com
IF(cond, val1, val2) is a MySQL built-in function that
returns the second argument if the first argument is TRUE, the third
argument otherwise.
The first argument is treated as an integer and evaluated to TRUE if it is not 0. For example:
SELECT IF(2>1, 'yes', 'no'), IF(9, 'yes', 'no');
-- +----------------------+--------------------+
-- | IF(2>1, 'yes', 'no') | IF(9, 'yes', 'no') |
-- +----------------------+--------------------+
-- | yes | yes |
-- +----------------------+--------------------+
SELECT IF(TRUE, 'yes', 'no'), IF('TRUE', 'yes', 'no'), IF('100% TRUE', 'yes', 'no');
-- +-----------------------+-------------------------+------------------------------+
-- | IF(TRUE, 'yes', 'no') | IF('TRUE', 'yes', 'no') | IF('100% TRUE', 'yes', 'no') |
-- +-----------------------+-------------------------+------------------------------+
-- | yes | no | yes |
-- +-----------------------+-------------------------+------------------------------+
SELECT IF(NULL, 'yes', 'no'), IF('NULL', 'yes', 'no');
-- +-----------------------+-------------------------+
-- | IF(TRUE, 'yes', 'no') | IF('True', 'yes', 'no') |
-- +-----------------------+-------------------------+
-- | yes | no |
-- +-----------------------+-------------------------+
Reference information of the IF() function:
IF(cond, val1, val2): val Returns the second argument if the first argument is TRUE, the third argument otherwise. Arguments, return value and availability: cond: Required. The Boolean condition. val1, val2: Required. The values to be selected from. val: Return value. The selected value. Available since MySQL 4.0.
⇒ IFNULL() - Replacing NULL Value
⇐ GREATEST() - Finding the Greatest/Maximum Value
2023-12-19, 1278🔥, 0💬
Popular Posts:
What Is the Difference Between GETDATE() and GETUTCDATE() in SQL Server Transact-SQL? The difference...
How To Calculate Age in Days, Hours and Minutes in SQL Server Transact-SQL? On many Web sites, news ...
How REAL and FLOAT Literal Values Are Rounded in SQL Server Transact-SQL? By definition, FLOAT(n) sh...
How To List All User Names in a Database in SQL Server? If you want to see a list of all user names ...
How to put statements into a statement block in SQL Server Transact-SQL? You can put statements into...