{"id":988,"date":"2013-01-13T20:26:36","date_gmt":"2013-01-14T04:26:36","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=988"},"modified":"2024-02-06T18:35:09","modified_gmt":"2024-02-07T01:35:09","slug":"mysql-case-statement","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/","title":{"rendered":"MySQL CASE Statement"},"content":{"rendered":"\n<p><strong>Summary<\/strong>:&nbsp;in this tutorial, you will learn how to use&nbsp;MySQL <code>CASE<\/code> statements to construct complex conditional statements inside stored procedures.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL CASE statement<\/h2>\n\n\n\n<p>Besides the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-if-statement\/\"><code>IF<\/code><\/a> statement, MySQL provides an\u00a0alternative\u00a0conditional\u00a0statement called the <code>CASE<\/code> statement used in stored procedures. The <code>CASE<\/code> statements make the code more readable and efficient.<\/p>\n\n\n\n<p>The <code>CASE<\/code> statement has two forms: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simple <code>CASE<\/code>  statement<\/li>\n\n\n\n<li>Searched <code>CASE<\/code> statement.<\/li>\n<\/ul>\n\n\n\n<p class=\"note\">Note that if you want to add the if-else logic to an SQL statement, you use the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/\"><code>CASE<\/code> expression<\/a> which differs from the <code>CASE<\/code> statement covered in this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Simple CASE statement<\/h2>\n\n\n\n<p>The following is the basic syntax of the simple <code>CASE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CASE case_value\n   WHEN when_value1 THEN statements\n   WHEN when_value2 THEN statements\n   ...\n   &#91;ELSE else-statements]\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">CASE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the simple <code>CASE<\/code> statement sequentially compares the <code>case_value<\/code> is with <code>when_value1<\/code>, <code>when_value2<\/code>, and so on until it finds a match. <\/p>\n\n\n\n<p>When the <code>CASE<\/code> finds a <code>case_value<\/code> that is equal to a <code>when_value<\/code>, it executes <code>statements<\/code> in the corresponding <code>THEN<\/code> clause.<\/p>\n\n\n\n<p>If the <code>CASE<\/code> statement cannot find any matches, it executes the <code>else-statements<\/code> in the <code>ELSE<\/code> clause if the <code>ELSE<\/code> clause is available.<\/p>\n\n\n\n<p>If the <code>ELSE<\/code> clause is not available and the <code>CASE<\/code> cannot find any matches, it&#8217;ll issue the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Case<\/span> not found <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-keyword\">CASE<\/span> statement<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notice that the <code>case_value<\/code> can be a literal value or an expression. The <code>statements<\/code> can consist of one or more SQL statements and cannot be empty.<\/p>\n\n\n\n<p>To avoid the error when the <code>case_value<\/code> does not equal any <code>when_value<\/code>, you can use an empty <code>BEGIN...END<\/code> block in the <code>ELSE<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CASE case_value\n    WHEN when_value1 THEN ...\n    WHEN when_value2 THEN ...\n    ELSE \n        <span class=\"hljs-keyword\">BEGIN<\/span>\n        <span class=\"hljs-keyword\">END<\/span>;\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">CASE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The simple <code>CASE<\/code> statement tests for equality ( <code>=<\/code>), therefore, you cannot use it to test equality with <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-null\/\">NULL<\/a><\/code> because <code>NULL<\/code> = <code>NULL<\/code> returns <code>FALSE<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Simple CASE statement example<\/h3>\n\n\n\n<p>The following stored procedure illustrates how to use the simple <code>CASE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">DELIMITER $$ \n\nCREATE PROCEDURE GetCustomerShipping(\n  IN pCustomerNumber INT, \n  OUT pShipping VARCHAR(<span class=\"hljs-number\">50<\/span>)\n) \nBEGIN \n\t<span class=\"hljs-keyword\">DECLARE<\/span> customerCountry VARCHAR(<span class=\"hljs-number\">100<\/span>);\n\tSELECT \n\t  country INTO customerCountry \n\tFROM \n\t  customers \n\tWHERE \n\t  customerNumber = pCustomerNumber;\n\n\t<span class=\"hljs-keyword\">CASE<\/span> customerCountry \n\t\tWHEN <span class=\"hljs-string\">'USA'<\/span> THEN \n\t\t\tSET pShipping = <span class=\"hljs-string\">'2-day Shipping'<\/span>;\n\t\tWHEN <span class=\"hljs-string\">'Canada'<\/span> THEN \n\t\t\tSET pShipping = <span class=\"hljs-string\">'3-day Shipping'<\/span>;\n\t\t<span class=\"hljs-keyword\">ELSE<\/span> \n\t\t\tSET pShipping = <span class=\"hljs-string\">'5-day Shipping'<\/span>;\n\tEND <span class=\"hljs-keyword\">CASE<\/span>;\nEND$$ \n\nDELIMITER ;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>The <code>GetCustomerShipping()<\/code> stored procedure accepts two parameters: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pCustomerNumber<\/code> as an <code>IN<\/code> parameter <\/li>\n\n\n\n<li><code>pShipping<\/code> as an <code>OUT<\/code> parameter.<\/li>\n<\/ul>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, retrieve the customer&#8217;s country by specified customer number from the <code>customers<\/code> table.<\/p>\n\n\n\n<p>Second, determine the shipping time based on the customer&#8217;s country using a simple <code>CASE<\/code> statement:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the customer locates in <code>USA<\/code> , the shipping time is <code>2-day shipping<\/code> . <\/li>\n\n\n\n<li>If the customer locates in <code>Canada<\/code> , the shipping time is <code>3-day shipping<\/code>. <\/li>\n\n\n\n<li>The customers from other countries will have to have <code>5-day shipping<\/code> .<\/li>\n<\/ul>\n\n\n\n<p>The following flowchart demonstrates the logic of the <code>CASE<\/code> statement for determining the shipping time:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement.png\"><img loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"431\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement.png\" alt=\"MySQL CASE statement flowchart\" class=\"wp-image-1024\" title=\"MySQL CASE statement flowchart\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement.png 440w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement-300x293.png 300w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/a><\/figure>\n\n\n\n<p>This statement calls the stored procedure with the customer id 112 and returns the shipping information:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CALL<\/span> GetCustomerShipping(<span class=\"hljs-number\">112<\/span>,@shipping);\n<span class=\"hljs-keyword\">SELECT<\/span> @shipping;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">+<span class=\"hljs-comment\">----------------+<\/span>\n| @shipping      |\n+<span class=\"hljs-comment\">----------------+<\/span>\n| 2-day Shipping |\n+<span class=\"hljs-comment\">----------------+<\/span>\n1 row in <span class=\"hljs-keyword\">set<\/span> (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Searched CASE statement<\/h2>\n\n\n\n<p>The simple <code>CASE<\/code> statement only allows you to compare a value with a set of distinct values.<\/p>\n\n\n\n<p>To perform more complex matches such as ranges, you use the other form of the CASE statement called the searched <code>CASE<\/code> statement. <\/p>\n\n\n\n<p>The searched <code>CASE<\/code> statement is equivalent to the <code>IF<\/code>\u00a0statement. However, it&#8217;s much more readable than the <code>IF<\/code> statement.<\/p>\n\n\n\n<p>Here&#8217;s the basic syntax of the searched <code>CASE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CASE\n    WHEN search_condition1 THEN statements\n    WHEN search_condition1 THEN statements\n    ...\n    &#91;ELSE else-statements]\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">CASE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, searched <code>CASE<\/code> evaluates each <code>search_condition<\/code> in the <code>WHEN<\/code> clause until it finds a condition that evaluates to <code>TRUE<\/code>, then it executes the corresponding <code>THEN<\/code> clause <code>statements<\/code>.<\/p>\n\n\n\n<p>If no <code>search_condition<\/code> evaluates to <code>TRUE<\/code>, the <code>CASE<\/code> will execute <code>else-statements<\/code> in the <code>ELSE<\/code> clause if an <code>ELSE<\/code> clause is available.<\/p>\n\n\n\n<p>Like a simple <code>CASE<\/code> statement, if you don&#8217;t specify an <code>ELSE<\/code> clause and no condition is <code>TRUE<\/code>, MySQL raises the same error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Case<\/span> not found <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-keyword\">CASE<\/span> statement<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>MySQL also does not allow you to have an empty <code>statements<\/code> in the <code>THEN<\/code> or <code>ELSE<\/code> clause. <\/p>\n\n\n\n<p>If you don&#8217;t want to handle the logic in the <code>ELSE<\/code> clause while preventing MySQL from raising an error in case no <code>search_condition<\/code> is true, you can use an empty <code>BEGIN...END<\/code> \u00a0block in the <code>ELSE<\/code> clause.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Searched CASE statement example<\/h3>\n\n\n\n<p>The following example demonstrates how to use a searched <code>CASE<\/code> statement to get the delivery status of an order based on the number of waiting days:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">DELIMITER $$ \n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> GetDeliveryStatus(\n  <span class=\"hljs-keyword\">IN<\/span> pOrderNumber <span class=\"hljs-built_in\">INT<\/span>, \n  <span class=\"hljs-keyword\">OUT<\/span> pDeliveryStatus <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>)\n) \n\n<span class=\"hljs-keyword\">BEGIN<\/span> \n\t<span class=\"hljs-comment\">-- get the waiting day from the orders table<\/span>\n\t<span class=\"hljs-keyword\">DECLARE<\/span> waitingDay <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-number\">0<\/span>;\n\t<span class=\"hljs-keyword\">SELECT<\/span> \n\t  <span class=\"hljs-keyword\">DATEDIFF<\/span>(shippedDate, requiredDate) <span class=\"hljs-keyword\">INTO<\/span> waitingDay \n\t<span class=\"hljs-keyword\">FROM<\/span> \n\t  orders \n\t<span class=\"hljs-keyword\">WHERE<\/span> \n\t  orderNumber = pOrderNumber;\n      \n\t<span class=\"hljs-comment\">-- determine delivery status\t  <\/span>\n\tCASE \n\t\tWHEN waitingDay &lt; 0 THEN \n\t\t\t<span class=\"hljs-keyword\">SET<\/span> pDeliveryStatus = <span class=\"hljs-string\">'Early Delivery'<\/span>;\n        WHEN waitingDay = 0 THEN \n\t\t\t<span class=\"hljs-keyword\">SET<\/span> pDeliveryStatus = <span class=\"hljs-string\">'On Time'<\/span>;\n\t\tWHEN waitingDay &gt;= 1 AND waitingDay &lt; 5 THEN \n\t\t\t<span class=\"hljs-keyword\">SET<\/span> pDeliveryStatus = <span class=\"hljs-string\">'Late'<\/span>;\n\t\tWHEN waitingDay &gt;= 5 THEN \n\t\t\t<span class=\"hljs-keyword\">SET<\/span> pDeliveryStatus = <span class=\"hljs-string\">'Very Late'<\/span>;\n\t\tELSE \n\t\t\t<span class=\"hljs-keyword\">SET<\/span> pDeliveryStatus = <span class=\"hljs-string\">'No Information'<\/span>;\n\t<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">CASE<\/span>;\n\n<span class=\"hljs-keyword\">END<\/span>$$ \n\nDELIMITER ;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>The stored procedure <code>GetDeliveryStatus()<\/code> accepts an order number as an <code>IN<\/code> parameter and returns the delivery status as an <code>OUT<\/code> parameter.<\/p>\n\n\n\n<p>First, calculate the waiting days between the required date and the shipped date.<\/p>\n\n\n\n<p>Second, determine the delivery status based on the number of waiting days using the searched <code>CASE<\/code> statement:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the number of waiting days is negative, then the delivery is early.<\/li>\n\n\n\n<li>If the number of waiting days is zero, then the delivery is on time.<\/li>\n\n\n\n<li>The delivery is late when the number of waiting days is between 1 and 5.<\/li>\n\n\n\n<li>When the number of waiting days is more than 5 days, then the delivery is very late.<\/li>\n\n\n\n<li>If the number of waiting days is NULL or else, the delivery has the status of no information specified in the <code>ELSE<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>This statement uses the stored procedure <code>GetDeliveryStatus()<\/code> to get the delivery status of the order <code>10100<\/code> :<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CALL<\/span> GetDeliveryStatus(<span class=\"hljs-number\">10100<\/span>,@delivery);\n<span class=\"hljs-keyword\">SELECT<\/span> @delivery;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----------------+\r\n| @delivery      |\r\n+----------------+\r\n| Early Delivery |\r\n+----------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">MySQL CASE statement vs. IF statement<\/h2>\n\n\n\n<p>Both <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-if-statement\/\">IF<\/a><\/code> and <code>CASE<\/code> statements allow you to execute a block of code based on a specific condition. Choosing between <code>IF<\/code> or <code>CASE<\/code> sometimes is just a matter of personal preference. <\/p>\n\n\n\n<p>Here are some general guidelines:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A simple <code>CASE<\/code> statement is more readable and efficient than an <code>IF<\/code> statement when you compare a single expression against a range of unique values. However, when checking complex expressions based on multiple values, the <code>IF<\/code> statement is easier to understand.<\/li>\n\n\n\n<li>When using the <code>CASE<\/code> statement, you need to make sure that at least one of the <code>CASE<\/code> condition is matched. Otherwise, you need to define an <a href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-declare-handler\/\">error handler<\/a> to catch the error. Note that you do not have to do this with the <code>IF<\/code> statement.<\/li>\n\n\n\n<li>In some situations, you can combine both <code>IF<\/code> and <code>CASE<\/code> to make the code more readable and efficient.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a simple <code>CASE<\/code> statement to evaluate a specific expression against a series of possible values and execute corresponding actions.<\/li>\n\n\n\n<li>Use a searched <code>CASE<\/code> statement to evaluate various conditions individually, allowing for a more flexible code.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"988\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/\"\n\t\t\t\tdata-post-title=\"MySQL CASE Statement\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"988\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/\"\n\t\t\t\tdata-post-title=\"MySQL CASE Statement\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the MySQL CASE statements, including simple CASE and searched CASE statement, in stored procedures.<\/p>\n","protected":false},"author":2,"featured_media":3363,"parent":518,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-988","page","type-page","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using MySQL CASE Statements in Stored Procedures<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the MySQL CASE statements, including simple CASE and searched CASE statement, in stored procedures.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using MySQL CASE Statements in Stored Procedures\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the MySQL CASE statements, including simple CASE and searched CASE statement, in stored procedures.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-07T01:35:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/mysql-case-statement\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/mysql-case-statement\\\/\",\"name\":\"Using MySQL CASE Statements in Stored Procedures\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/mysql-case-statement\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/mysql-case-statement\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/01\\\/mysql-case-statement.jpg\",\"datePublished\":\"2013-01-14T04:26:36+00:00\",\"dateModified\":\"2024-02-07T01:35:09+00:00\",\"description\":\"This tutorial shows you how to use the MySQL CASE statements, including simple CASE and searched CASE statement, in stored procedures.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/mysql-case-statement\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/mysql-case-statement\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/mysql-case-statement\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/01\\\/mysql-case-statement.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/01\\\/mysql-case-statement.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/mysql-case-statement\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Stored Procedures\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL CASE Statement\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using MySQL CASE Statements in Stored Procedures","description":"This tutorial shows you how to use the MySQL CASE statements, including simple CASE and searched CASE statement, in stored procedures.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/","og_locale":"en_US","og_type":"article","og_title":"Using MySQL CASE Statements in Stored Procedures","og_description":"This tutorial shows you how to use the MySQL CASE statements, including simple CASE and searched CASE statement, in stored procedures.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-02-07T01:35:09+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement.jpg","type":"image\/jpeg"}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/","url":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/","name":"Using MySQL CASE Statements in Stored Procedures","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement.jpg","datePublished":"2013-01-14T04:26:36+00:00","dateModified":"2024-02-07T01:35:09+00:00","description":"This tutorial shows you how to use the MySQL CASE statements, including simple CASE and searched CASE statement, in stored procedures.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement.jpg","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/01\/mysql-case-statement.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Stored Procedures","item":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/"},{"@type":"ListItem","position":3,"name":"MySQL CASE Statement"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=988"}],"version-history":[{"count":4,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/988\/revisions"}],"predecessor-version":[{"id":14631,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/988\/revisions\/14631"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/518"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media\/3363"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}