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, 2666🔥, 0💬
Popular Posts:
How to execute statements in loops in SQL Server Transact-SQL? How to use WHILE ... loops? You can u...
How To Break Query Output into Pages in MySQL? If you have a query that returns hundreds of rows, an...
How To View Data Files in the Current Database in Oracle? If you want to get a list of all tablespac...
Where to find answers to frequently asked questions in general areas of Microsoft SQL Server Transac...
How to check if two JSON values have overlaps using the JSON_OVERLAPS() function? JSON_OVERLAPS(json...