{"id":1532,"date":"2025-01-08T08:08:40","date_gmt":"2025-01-08T01:08:40","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=1532"},"modified":"2025-01-08T08:58:17","modified_gmt":"2025-01-08T01:58:17","slug":"plpgsql-case","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/","title":{"rendered":"PL\/pgSQL CASE Statement"},"content":{"rendered":"\n<p><strong>Summary:<\/strong> In this tutorial, you&#8217;ll learn how to use the PL\/pgSQL <code>CASE<\/code> statement to execute a block of code based on conditions.<\/p>\n\n\n\n<p>The <code>CASE<\/code> statement allows you to execute code based on conditions.<\/p>\n\n\n\n<p>PL\/pgSQL offers two types of <code>CASE<\/code> statements:<\/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<h2 class=\"wp-block-heading\" id='simple-case-statement'>Simple CASE statement <a href=\"#simple-case-statement\" class=\"anchor\" id=\"simple-case-statement\" title=\"Anchor for Simple CASE statement\">#<\/a><\/h2>\n\n\n\n<p>Here&#8217;s the 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=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CASE<\/span> expression\n    <span class=\"hljs-keyword\">WHEN<\/span> value1 <span class=\"hljs-keyword\">THEN<\/span>\n        statements\n    <span class=\"hljs-keyword\">WHEN<\/span> value2 <span class=\"hljs-keyword\">THEN<\/span>\n        statements\n    ...\n    <span class=\"hljs-keyword\">ELSE<\/span>\n        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\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The simple <code>CASE<\/code> statement compares an <code>expression<\/code> with <code>values<\/code> (<code>value1<\/code>, <code>value2<\/code>, etc.) sequentially from top to bottom.<\/p>\n\n\n\n<p>If the result of the <code>expression<\/code> equals a <code>value<\/code>, the <code>CASE<\/code> statement executes the corresponding code and stops comparing the <code>expression<\/code> with the remaining <code>values<\/code>.<\/p>\n\n\n\n<p>If the result of the <code>expression<\/code> does not equal any <code>values<\/code>, the <code>CASE<\/code> statement executes the code block in the <code>ELSE<\/code> branch.<\/p>\n\n\n\n<p>The <code>ELSE<\/code> branch is optional. If you omit the <code>ELSE<\/code> branch and the <code>expression<\/code> does not equal any <code>value<\/code>, the simple <code>CASE<\/code> statement raises a <code>CASE_NOT_FOUND<\/code> exception.<\/p>\n\n\n\n<p>The following example <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-function\/\">defines a new function<\/a> called <code>get_storage_cost<\/code> that returns a storage cost based on a warehouse:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR REPLACE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> get_storage_cost(<span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>) \n<span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-type\">DEC<\/span>\n<span class=\"hljs-keyword\">AS<\/span>\n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_storage_cost <span class=\"hljs-type\">DEC<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">CASE<\/span> <span class=\"hljs-type\">name<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-string\">'San Jose Warehouse'<\/span> <span class=\"hljs-keyword\">THEN<\/span> v_storage_cost = <span class=\"hljs-number\">10<\/span>;\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-string\">'San Francisco Warehouse'<\/span> <span class=\"hljs-keyword\">THEN<\/span> v_storage_cost = <span class=\"hljs-number\">15<\/span>;\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-string\">'Los Angeles Warehouse'<\/span> <span class=\"hljs-keyword\">THEN<\/span> v_storage_cost = <span class=\"hljs-number\">12<\/span>;\n        <span class=\"hljs-keyword\">ELSE<\/span>\n            v_storage_cost = <span class=\"hljs-number\">5<\/span>;\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">CASE<\/span>;\n <span class=\"hljs-keyword\">RETURN<\/span> v_storage_cost;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span>\n<span class=\"hljs-keyword\">LANGUAGE<\/span> PLPGSQL;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=Q1JFQVRFIE9SIFJFUExBQ0UgRlVOQ1RJT04gZ2V0X3N0b3JhZ2VfY29zdChuYW1lIFZBUkNIQVIpIApSRVRVUk5TIERFQwpBUwokJApERUNMQVJFCiAgICB2X3N0b3JhZ2VfY29zdCBERUM7CkJFR0lOCiAgICBDQVNFIG5hbWUKICAgICAgICBXSEVOICdTYW4gSm9zZSBXYXJlaG91c2UnIFRIRU4gdl9zdG9yYWdlX2Nvc3QgPSAxMDsKICAgICAgICBXSEVOICdTYW4gRnJhbmNpc2NvIFdhcmVob3VzZScgVEhFTiB2X3N0b3JhZ2VfY29zdCA9IDE1OwogICAgICAgIFdIRU4gJ0xvcyBBbmdlbGVzIFdhcmVob3VzZScgVEhFTiB2X3N0b3JhZ2VfY29zdCA9IDEyOwogICAgICAgIEVMU0UKICAgICAgICAgICAgdl9zdG9yYWdlX2Nvc3QgPSA1OwogICAgRU5EIENBU0U7CiBSRVRVUk4gdl9zdG9yYWdlX2Nvc3Q7CkVORDsKJCQKTEFOR1VBR0UgUExQR1NRTDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this <code>get_storage_cost<\/code> function, we use the simple <code>CASE<\/code> statement to return a different storage cost based on the warehouse name.<\/p>\n\n\n\n<p>The following statement calls the <code>get_storage_cost<\/code> function to get the storage cost of the San Jose Warehouse:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> get_storage_cost(<span class=\"hljs-string\">'San Jose Warehouse'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/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-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> get_storage_cost\n<span class=\"hljs-comment\">------------------<\/span>\n               <span class=\"hljs-number\">10<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='searched-case-statement'>Searched CASE statement <a href=\"#searched-case-statement\" class=\"anchor\" id=\"searched-case-statement\" title=\"Anchor for Searched CASE statement\">#<\/a><\/h2>\n\n\n\n<p>Here&#8217;s the syntax of the searched <code>CASE<\/code> expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CASE<\/span>\n    <span class=\"hljs-keyword\">WHEN<\/span> boolean_expression_1 <span class=\"hljs-keyword\">THEN<\/span>\n        statements;\n    <span class=\"hljs-keyword\">WHEN<\/span> boolean_expression_2 <span class=\"hljs-keyword\">THEN<\/span>\n        statements;\n    ...\n    <span class=\"hljs-keyword\">ELSE<\/span>\n        statements;\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">CASE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The searched <code>CASE<\/code> statement evaluates a set of boolean expressions.<\/p>\n\n\n\n<p>If an expression is <code>true<\/code>, the <code>CASE<\/code> statement executes the corresponding code block and stops evaluating the remaining boolean expressions.<\/p>\n\n\n\n<p>If no expression is <code>true<\/code>, the <code>CASE<\/code> statement executes the <code>ELSE<\/code> branch.<\/p>\n\n\n\n<p>Since the <code>ELSE<\/code> branch is optional, you can omit it. However, if no expression is <code>true<\/code> and the <code>ELSE<\/code> branch is unavailable, the <code>CASE<\/code> statement will raise a <code>CASE_NOT_FOUND<\/code> exception.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR REPLACE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> get_weight_type(id <span class=\"hljs-type\">INT<\/span>)\n<span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-type\">VARCHAR<\/span>\n<span class=\"hljs-keyword\">AS<\/span>\n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_gross_weight products.gross_weight<span class=\"hljs-meta\">%TYPE<\/span>;\n    v_weight_type <span class=\"hljs-type\">VARCHAR<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> gross_weight <span class=\"hljs-keyword\">INTO<\/span> v_gross_weight\n    <span class=\"hljs-keyword\">FROM<\/span> products\n    <span class=\"hljs-keyword\">WHERE<\/span> product_id = id;\n\n    <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-built_in\">FOUND<\/span> <span class=\"hljs-keyword\">THEN<\/span>\n        <span class=\"hljs-keyword\">RETURN<\/span> <span class=\"hljs-keyword\">NULL<\/span>;\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n\n    <span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> v_gross_weight &lt; <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">THEN<\/span> v_weight_type = <span class=\"hljs-string\">'Small'<\/span>;\n        <span class=\"hljs-keyword\">WHEN<\/span> v_gross_weight &gt;= <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">AND<\/span> v_gross_weight &lt; <span class=\"hljs-number\">5<\/span> <span class=\"hljs-keyword\">THEN<\/span> v_weight_type = <span class=\"hljs-string\">'Medium'<\/span>;\n        <span class=\"hljs-keyword\">ELSE<\/span> v_weight_type = <span class=\"hljs-string\">'Big'<\/span>;\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">CASE<\/span>;\n    <span class=\"hljs-keyword\">RETURN<\/span> v_weight_type;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span> <span class=\"hljs-keyword\">LANGUAGE<\/span> plpgsql;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=Q1JFQVRFIE9SIFJFUExBQ0UgRlVOQ1RJT04gZ2V0X3dlaWdodF90eXBlKGlkIElOVCkKUkVUVVJOUyBWQVJDSEFSCkFTCiQkCkRFQ0xBUkUKICAgIHZfZ3Jvc3Nfd2VpZ2h0IHByb2R1Y3RzLmdyb3NzX3dlaWdodCVUWVBFOwogICAgdl93ZWlnaHRfdHlwZSBWQVJDSEFSOwpCRUdJTgogICAgU0VMRUNUIGdyb3NzX3dlaWdodCBJTlRPIHZfZ3Jvc3Nfd2VpZ2h0CiAgICBGUk9NIHByb2R1Y3RzCiAgICBXSEVSRSBwcm9kdWN0X2lkID0gaWQ7CgogICAgSUYgTk9UIEZPVU5EIFRIRU4KICAgICAgICBSRVRVUk4gTlVMTDsKICAgIEVORCBJRjsKCiAgICBDQVNFCiAgICAgICAgV0hFTiB2X2dyb3NzX3dlaWdodCA8IDEgVEhFTiB2X3dlaWdodF90eXBlID0gJ1NtYWxsJzsKICAgICAgICBXSEVOIHZfZ3Jvc3Nfd2VpZ2h0ID49IDEgQU5EIHZfZ3Jvc3Nfd2VpZ2h0IDwgNSBUSEVOIHZfd2VpZ2h0X3R5cGUgPSAnTWVkaXVtJzsKICAgICAgICBFTFNFIHZfd2VpZ2h0X3R5cGUgPSAnQmlnJzsKICAgIEVORCBDQVNFOwogICAgUkVUVVJOIHZfd2VpZ2h0X3R5cGU7CkVORDsKJCQgTEFOR1VBR0UgcGxwZ3NxbDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How the function works:<\/p>\n\n\n\n<p>First, retrieve the <code>gross_weight<\/code> of a product specified by <code>id<\/code> from the <code>products<\/code> table and assign it to the <code>v_gross_weight<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> gross_weight <span class=\"hljs-keyword\">INTO<\/span> v_gross_weight \n<span class=\"hljs-keyword\">FROM<\/span> products \n<span class=\"hljs-keyword\">WHERE<\/span> product_id = id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, return <code>NULL<\/code> if the product with the <code>id<\/code> does not exist:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-built_in\">FOUND<\/span> <span class=\"hljs-keyword\">THEN<\/span> \n    <span class=\"hljs-keyword\">RETURN<\/span> <span class=\"hljs-keyword\">NULL<\/span>; \n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, classify the weight of the product based on the gross weight using the searched <code>CASE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CASE<\/span>\n    <span class=\"hljs-keyword\">WHEN<\/span> v_gross_weight &lt; <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">THEN<\/span> v_weight_type = <span class=\"hljs-string\">'Small'<\/span>;\n    <span class=\"hljs-keyword\">WHEN<\/span> v_gross_weight &gt;= <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">AND<\/span> v_gross_weight &lt; <span class=\"hljs-number\">5<\/span> <span class=\"hljs-keyword\">THEN<\/span> v_weight_type = <span class=\"hljs-string\">'Medium'<\/span>;\n    <span class=\"hljs-keyword\">ELSE<\/span> v_weight_type = <span class=\"hljs-string\">'Big'<\/span>;\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">CASE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>CASE<\/code> statement checks the <code>gross_weight<\/code> and assigns the value to the <code>v_weight_type<\/code> accordingly.<\/p>\n\n\n\n<p>Finally, return the <code>v_weight_type<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">RETURN<\/span> v_weight_type;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement calls the <code>get_weight_type()<\/code> function to get the weight type of the product with id 1:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> get_weight_type(<span class=\"hljs-number\">1<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGdldF93ZWlnaHRfdHlwZSgxKTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> get_weight_type\n<span class=\"hljs-comment\">-----------------<\/span>\n Small<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'><strong>Summary<\/strong> <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for &lt;strong&gt;Summary&lt;\/strong&gt;\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the simple <code>CASE<\/code> statement to compare an expression to a set of values and execute the corresponding code.<\/li>\n\n\n\n<li>Use the searched <code>CASE<\/code> statement to evaluate a set of boolean expressions and execute the corresponding code for the first true expression.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=plpgsql-case\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\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=\"1532\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/\"\n\t\t\t\tdata-post-title=\"PL\/pgSQL 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=\"1532\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/\"\n\t\t\t\tdata-post-title=\"PL\/pgSQL 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>In this tutorial, you&#8217;ll learn how to use the PL\/pgSQL CASE statement to execute a block of code based on conditions.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1420,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1532","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PL\/pgSQL CASE<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PL\/pgSQL CASE statement to execute a block of code based on conditions.\" \/>\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.pgtutorial.com\/plpgsql\/plpgsql-case\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PL\/pgSQL CASE\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PL\/pgSQL CASE statement to execute a block of code based on conditions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-08T01:58:17+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-case\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-case\\\/\",\"name\":\"PL\\\/pgSQL CASE\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2025-01-08T01:08:40+00:00\",\"dateModified\":\"2025-01-08T01:58:17+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PL\\\/pgSQL CASE statement to execute a block of code based on conditions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-case\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-case\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-case\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PL\\\/pgSQL\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PL\\\/pgSQL CASE Statement\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?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":"PL\/pgSQL CASE","description":"In this tutorial, you'll learn how to use the PL\/pgSQL CASE statement to execute a block of code based on conditions.","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.pgtutorial.com\/plpgsql\/plpgsql-case\/","og_locale":"en_US","og_type":"article","og_title":"PL\/pgSQL CASE","og_description":"In this tutorial, you'll learn how to use the PL\/pgSQL CASE statement to execute a block of code based on conditions.","og_url":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-08T01:58:17+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/","url":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/","name":"PL\/pgSQL CASE","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2025-01-08T01:08:40+00:00","dateModified":"2025-01-08T01:58:17+00:00","description":"In this tutorial, you'll learn how to use the PL\/pgSQL CASE statement to execute a block of code based on conditions.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PL\/pgSQL","item":"https:\/\/www.pgtutorial.com\/plpgsql\/"},{"@type":"ListItem","position":3,"name":"PL\/pgSQL CASE Statement"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=1532"}],"version-history":[{"count":9,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1532\/revisions"}],"predecessor-version":[{"id":1545,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1532\/revisions\/1545"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1420"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=1532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}