Collections:
Using CASE Expression in MySQL
How To Use CASE Expression in MySQL?
✍: FYIcenter.com
There are 2 ways to use the CASE expression. The first way is to return one of the predefined values based on the comparison of a given value to a list of target values. The second way is to return one of the predefined values based on a list of conditions. Here is the syntax of both types of CASE expressions:
CASE value WHEN target_value THEN result WHEN target_value THEN result WHEN target_value THEN result ... ELSE result END CASE WHEN condition THEN result WHEN condition THEN result WHEN condition THEN result ... ELSE result END
The tutorial exercise below gives two good examples:
SELECT CASE 'Sun' WHEN 'Mon' THEN 'Open' WHEN "Fri" THEN "Open" ELSE 'Closed' END FROM DUAL; Closed SELECT CASE WHEN HOUR(CURRENT_TIME())<9 THEN 'Closed' WHEN HOUR(CURRENT_TIME())>17 THEN 'Closed' ELSE 'Open' END FROM DUAL; Closed
⇒ Introduction to Date and Time Handling in MySQL
⇐ Using Regular Expression with REGEXP in MySQL
2018-03-24, 3326🔥, 0💬
Popular Posts:
How to set database to be READ_ONLY in SQL Server? Databases in SQL Server have two update options: ...
How To Generate CREATE TABLE Script on an Existing Table in SQL Server? If you want to know how an e...
How To Drop an Index in Oracle? If you don't need an existing index any more, you should delete it w...
Where to find tutorials to answer some frequently asked questions on Microsoft SQL Server Transact-S...
How To Change the Password for Your Own User Account in MySQL? If you want to change the password of...