<p>How do I perform an <code>IF...THEN</code> in an <code>SQL SELECT</code> statement?</p>

<p>For example:</p>

<pre><code>SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
</code></pre>

[{'answer_id': 63474, 'author': 'John Sheehan', 'author_id': 1786, 'author_profile': 'https://Stackoverflow.com/users/1786', 'pm_score': 6, 'selected': False, 'text': "<pre><code>SELECT  \n(CASE \n     WHEN (Obsolete = 'N' OR InStock = 'Y') THEN 'YES'\n                                            ELSE 'NO' \n END) as Salable\n, * \nFROM Product\n</code></pre>\n"}, {'answer_id': 63476, 'author': 'palehorse', 'author_id': 312, 'author_profile': 'https://Stackoverflow.com/users/312', 'pm_score': 6, 'selected': False, 'text': "<p>Use CASE.  Something like this.</p>\n\n<pre><code>SELECT Salable =\n        CASE Obsolete\n        WHEN 'N' THEN 1\n        ELSE 0\n    END\n</code></pre>\n"}, {'answer_id': 63477, 'author': 'Santiago Cepas', 'author_id': 6547, 'author_profile': 'https://Stackoverflow.com/users/6547', 'pm_score': 6, 'selected': False, 'text': "<pre><code> SELECT\n   CASE \n      WHEN OBSOLETE = 'N' or InStock = 'Y' THEN 'TRUE' \n      ELSE 'FALSE' \n   END AS Salable,\n   * \nFROM PRODUCT\n</code></pre>\n"}, {'answer_id': 63480, 'author': 'Darrel Miller', 'author_id': 6819, 'author_profile': 'https://Stackoverflow.com/users/6819', 'pm_score': 12, 'selected': True, 'text': '<p>The <code>CASE</code> statement is the closest to IF in SQL and is supported on all versions of SQL Server.</p>\n<pre><code>SELECT CAST(\n             CASE\n                  WHEN Obsolete = \'N\' or InStock = \'Y\'\n                     THEN 1\n                  ELSE 0\n             END AS bit) as Saleable, *\nFROM Product\n</code></pre>\n<p>You only need to use the <code>CAST</code> operator if you want the result as a Boolean value. If you are happy with an <code>int</code>, this works:</p>\n<pre><code>SELECT CASE\n            WHEN Obsolete = \'N\' or InStock = \'Y\'\n               THEN 1\n               ELSE 0\n       END as Saleable, *\nFROM Product\n</code></pre>\n<p><code>CASE</code> statements can be embedded in other <code>CASE</code> statements and even included in aggregates.</p>\n<p>SQL Server Denali (SQL Server 2012) adds the <a href="http://msdn.microsoft.com/en-us/library/hh213574%28v=sql.110%29.aspx" rel="noreferrer">IIF</a> statement which is also available in <a href="http://www.techonthenet.com/access/functions/advanced/iif.php" rel="noreferrer">access</a> (pointed out by <a href="https://stackoverflow.com/questions/63447/how-do-you-perform-an-if-then-in-an-sql-select/6769805#6769805">Martin Smith</a>):</p>\n<pre><code>SELECT IIF(Obsolete = \'N\' or InStock = \'Y\', 1, 0) as Saleable, * FROM Product\n</code></pre>\n'}, {'answer_id': 63498, 'author': 'sven', 'author_id': 46, 'author_profile': 'https://Stackoverflow.com/users/46', 'pm_score': 7, 'selected': False, 'text': '<p>You can find some nice examples in <em><a href="https://web.archive.org/web/20210728081626/https://www.4guysfromrolla.com/webtech/102704-1.shtml" rel="nofollow noreferrer">The Power of SQL CASE Statements</a></em>, and I think the statement that you can use will be something like this (from <a href="https://web.archive.org/web/20211020202742/https://www.4guysfromrolla.com" rel="nofollow noreferrer">4guysfromrolla</a>):</p>\n<pre><code>SELECT\n    FirstName, LastName,\n    Salary, DOB,\n    CASE Gender\n        WHEN \'M\' THEN \'Male\'\n        WHEN \'F\' THEN \'Female\'\n    END\nFROM Employees\n</code></pre>\n'}, {'answer_id': 63500, 'author': 'user7658', 'author_id': 7658, 'author_profile': 'https://Stackoverflow.com/users/7658', 'pm_score': 6, 'selected': False, 'text': "<p>Microsoft SQL Server (T-SQL)</p>\n\n<p>In a <code>select</code>, use:</p>\n\n<pre><code>select case when Obsolete = 'N' or InStock = 'Y' then 'YES' else 'NO' end\n</code></pre>\n\n<p>In a <code>where</code> clause, use:</p>\n\n<pre><code>where 1 = case when Obsolete = 'N' or InStock = 'Y' then 1 else 0 end\n</code></pre>\n"}, {'answer_id': 63504, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': "<p>Use a CASE statement:</p>\n\n<pre><code>SELECT CASE\n       WHEN (Obsolete = 'N' OR InStock = 'Y')\n       THEN 'Y'\n       ELSE 'N'\nEND as Available\n\netc...\n</code></pre>\n"}, {'answer_id': 63777, 'author': 'Jonathan', 'author_id': 6910, 'author_profile': 'https://Stackoverflow.com/users/6910', 'pm_score': 8, 'selected': False, 'text': '<p>The case statement is your friend in this situation, and takes one of two forms:</p>\n\n<p>The simple case:</p>\n\n<pre><code>SELECT CASE &lt;variable&gt; WHEN &lt;value&gt;      THEN &lt;returnvalue&gt;\n                       WHEN &lt;othervalue&gt; THEN &lt;returnthis&gt;\n                                         ELSE &lt;returndefaultcase&gt;\n       END AS &lt;newcolumnname&gt;\nFROM &lt;table&gt;\n</code></pre>\n\n<p>The extended case:</p>\n\n<pre><code>SELECT CASE WHEN &lt;test&gt;      THEN &lt;returnvalue&gt;\n            WHEN &lt;othertest&gt; THEN &lt;returnthis&gt;\n                             ELSE &lt;returndefaultcase&gt;\n       END AS &lt;newcolumnname&gt;\nFROM &lt;table&gt;\n</code></pre>\n\n<p>You can even put case statements in an order by clause for really fancy ordering.</p>\n'}, {'answer_id': 2010311, 'author': 'Ken', 'author_id': 244385, 'author_profile': 'https://Stackoverflow.com/users/244385', 'pm_score': 6, 'selected': False, 'text': '<p>From <a href="http://www.databasejournal.com/features/mssql/article.php/3087431/T-SQL-Programming-Part-1---Defining-Variables-and-IFELSE-logic.htm" rel="noreferrer">this link</a>, we can understand <code>IF THEN ELSE</code> in T-SQL:</p>\n\n<pre><code>IF EXISTS(SELECT *\n          FROM   Northwind.dbo.Customers\n          WHERE  CustomerId = \'ALFKI\')\n  PRINT \'Need to update Customer Record ALFKI\'\nELSE\n  PRINT \'Need to add Customer Record ALFKI\'\n\nIF EXISTS(SELECT *\n          FROM   Northwind.dbo.Customers\n          WHERE  CustomerId = \'LARSE\')\n  PRINT \'Need to update Customer Record LARSE\'\nELSE\n  PRINT \'Need to add Customer Record LARSE\' \n</code></pre>\n\n<p>Isn\'t this good enough for T-SQL?</p>\n'}, {'answer_id': 6769805, 'author': 'Martin Smith', 'author_id': 73226, 'author_profile': 'https://Stackoverflow.com/users/73226', 'pm_score': 8, 'selected': False, 'text': '<p>From SQL Server 2012 you can use the <a href="http://msdn.microsoft.com/en-us/library/hh213574(v=sql.110).aspx" rel="noreferrer"><strong><code>IIF</code></strong> function</a> for this.</p>\n\n<pre><code>SELECT IIF(Obsolete = \'N\' OR InStock = \'Y\', 1, 0) AS Salable, *\nFROM   Product\n</code></pre>\n\n<p>This is effectively just a shorthand (albeit not standard SQL) way of writing <code>CASE</code>.</p>\n\n<p>I prefer the conciseness when compared with the expanded <code>CASE</code> version.</p>\n\n<p>Both <code>IIF()</code> and <code>CASE</code> resolve as expressions within a SQL statement and can only be used in well-defined places.</p>\n\n<blockquote>\n  <p>The CASE expression cannot be used to control the flow of execution of\n  Transact-SQL statements, statement blocks, user-defined functions, and\n  stored procedures.</p>\n</blockquote>\n\n<p>If your needs can not be satisfied by these limitations (for example, a need to return differently shaped result sets dependent on some condition) then SQL Server does also have a procedural <a href="http://msdn.microsoft.com/en-gb/library/ms182717.aspx" rel="noreferrer"><code>IF</code></a> keyword.</p>\n\n<pre><code>IF @IncludeExtendedInformation = 1\n  BEGIN\n      SELECT A,B,C,X,Y,Z\n      FROM   T\n  END\nELSE\n  BEGIN\n      SELECT A,B,C\n      FROM   T\n  END\n</code></pre>\n\n<p><a href="https://dba.stackexchange.com/a/9874/3690">Care must sometimes be taken to avoid parameter sniffing issues with this approach however</a>.</p>\n'}, {'answer_id': 13089905, 'author': 'Robert B. Grossman', 'author_id': 1777537, 'author_profile': 'https://Stackoverflow.com/users/1777537', 'pm_score': 4, 'selected': False, 'text': '<p>If you\'re inserting results into a table for the first time, rather than transferring results from one table to another, this works in <a href="https://en.wikipedia.org/wiki/Oracle_Database#Releases_and_versions" rel="nofollow noreferrer">Oracle 11.2g</a>:</p>\n<pre><code>INSERT INTO customers (last_name, first_name, city)\n    SELECT \'Doe\', \'John\', \'Chicago\' FROM dual\n    WHERE NOT EXISTS\n        (SELECT \'1\' from customers\n            where last_name = \'Doe\'\n            and first_name = \'John\'\n            and city = \'Chicago\');\n</code></pre>\n'}, {'answer_id': 17004447, 'author': 'Tomasito', 'author_id': 1296687, 'author_profile': 'https://Stackoverflow.com/users/1296687', 'pm_score': 5, 'selected': False, 'text': '<p>Use pure bit logic:</p>\n\n<pre><code>DECLARE @Product TABLE (\n    id INT PRIMARY KEY IDENTITY NOT NULL\n   ,Obsolote CHAR(1)\n   ,Instock CHAR(1)\n)\n\nINSERT INTO @Product ([Obsolote], [Instock])\n    VALUES (\'N\', \'N\'), (\'N\', \'Y\'), (\'Y\', \'Y\'), (\'Y\', \'N\')\n\n;\nWITH cte\nAS\n(\n    SELECT\n        \'CheckIfInstock\' = CAST(ISNULL(NULLIF(ISNULL(NULLIF(p.[Instock], \'Y\'), 1), \'N\'), 0) AS BIT)\n       ,\'CheckIfObsolote\' = CAST(ISNULL(NULLIF(ISNULL(NULLIF(p.[Obsolote], \'N\'), 0), \'Y\'), 1) AS BIT)\n       ,*\n    FROM\n        @Product AS p\n)\nSELECT\n    \'Salable\' = c.[CheckIfInstock] &amp; ~c.[CheckIfObsolote]\n   ,*\nFROM\n    [cte] c\n</code></pre>\n\n<p>See <a href="http://sqlfiddle.com/#!3/0b900/3/0" rel="nofollow noreferrer">working demo: if then without <code>case</code> in SQL&nbsp;Server</a>.</p>\n\n<p>For start, you need to work out the value of <code>true</code> and <code>false</code> for selected conditions. Here comes two <a href="https://learn.microsoft.com/en-us/sql/t-sql/language-elements/nullif-transact-sql?view=sql-server-2017" rel="nofollow noreferrer">NULLIF</a>:</p>\n\n<pre><code>for true: ISNULL(NULLIF(p.[Instock], \'Y\'), 1)\nfor false: ISNULL(NULLIF(p.[Instock], \'N\'), 0)\n</code></pre>\n\n<p>combined together gives 1 or 0. Next use <a href="http://msdn.microsoft.com/en-us/library/ms176122.aspx" rel="nofollow noreferrer">bitwise operators</a>.</p>\n\n<p>It\'s the most <a href="http://en.wikipedia.org/wiki/WYSIWYG" rel="nofollow noreferrer">WYSIWYG</a> method.</p>\n'}, {'answer_id': 20992729, 'author': 'Dibin', 'author_id': 1915236, 'author_profile': 'https://Stackoverflow.com/users/1915236', 'pm_score': 3, 'selected': False, 'text': "<p>For those who uses SQL Server 2012, IIF is a feature that has been added and works as an alternative to Case statements.</p>\n\n<pre><code>SELECT IIF(Obsolete = 'N' OR InStock = 'Y', 1, 0) AS Salable, *\nFROM   Product \n</code></pre>\n"}, {'answer_id': 32200662, 'author': 'Mohammad Atiour Islam', 'author_id': 1077346, 'author_profile': 'https://Stackoverflow.com/users/1077346', 'pm_score': 4, 'selected': False, 'text': "<pre><code>SELECT CASE WHEN profile.nrefillno = 0 THEN 'N' ELSE 'R'END as newref\nFrom profile\n</code></pre>\n"}, {'answer_id': 34178590, 'author': 'Chanukya', 'author_id': 5093602, 'author_profile': 'https://Stackoverflow.com/users/5093602', 'pm_score': 4, 'selected': False, 'text': "<pre><code>case statement some what similar to if in SQL server\n\nSELECT CASE \n            WHEN Obsolete = 'N' or InStock = 'Y' \n               THEN 1 \n               ELSE 0 \n       END as Saleable, * \nFROM Product\n</code></pre>\n"}, {'answer_id': 34340652, 'author': 'Ravi Anand', 'author_id': 2444505, 'author_profile': 'https://Stackoverflow.com/users/2444505', 'pm_score': 5, 'selected': False, 'text': "<p>Simple if-else statement in SQL&nbsp;Server:</p>\n\n<pre><code>DECLARE @val INT;\nSET @val = 15;\n\nIF @val &lt; 25\nPRINT 'Hi Ravi Anand';\nELSE\nPRINT 'By Ravi Anand.';\n\nGO\n</code></pre>\n\n<p>Nested If...else statement in SQL&nbsp;Server -</p>\n\n<pre><code>DECLARE @val INT;\nSET @val = 15;\n\nIF @val &lt; 25\nPRINT 'Hi Ravi Anand.';\nELSE\nBEGIN\nIF @val &lt; 50\n  PRINT 'what''s up?';\nELSE\n  PRINT 'Bye Ravi Anand.';\nEND;\n\nGO\n</code></pre>\n"}, {'answer_id': 35350520, 'author': 'JustJohn', 'author_id': 564810, 'author_profile': 'https://Stackoverflow.com/users/564810', 'pm_score': 4, 'selected': False, 'text': "<p>This isn't an answer, just an example of a CASE statement in use where I work. It has a nested CASE statement. Now you know why my eyes are crossed. </p>\n\n<pre><code> CASE orweb2.dbo.Inventory.RegulatingAgencyName\n    WHEN 'Region 1'\n        THEN orweb2.dbo.CountyStateAgContactInfo.ContactState\n    WHEN 'Region 2'\n        THEN orweb2.dbo.CountyStateAgContactInfo.ContactState\n    WHEN 'Region 3'\n        THEN orweb2.dbo.CountyStateAgContactInfo.ContactState\n    WHEN 'DEPT OF AGRICULTURE'\n        THEN orweb2.dbo.CountyStateAgContactInfo.ContactAg\n    ELSE (\n            CASE orweb2.dbo.CountyStateAgContactInfo.IsContract\n                WHEN 1\n                    THEN orweb2.dbo.CountyStateAgContactInfo.ContactCounty\n                ELSE orweb2.dbo.CountyStateAgContactInfo.ContactState\n                END\n            )\n    END AS [County Contact Name]\n</code></pre>\n"}, {'answer_id': 36868923, 'author': 'sandeep rawat', 'author_id': 6085803, 'author_profile': 'https://Stackoverflow.com/users/6085803', 'pm_score': 5, 'selected': False, 'text': '<p>A new feature, <a href="https://msdn.microsoft.com/en-IN/library/hh213574.aspx" rel="noreferrer">IIF</a> (that we can simply use), was added in SQL Server 2012:</p>\n\n<pre><code>SELECT IIF ( (Obsolete = \'N\' OR InStock = \'Y\'), 1, 0) AS Saleable, * FROM Product\n</code></pre>\n'}, {'answer_id': 37167739, 'author': 'onedaywhen', 'author_id': 15354, 'author_profile': 'https://Stackoverflow.com/users/15354', 'pm_score': 4, 'selected': False, 'text': "<pre><code>SELECT 1 AS Saleable, *\n  FROM @Product\n WHERE ( Obsolete = 'N' OR InStock = 'Y' )\nUNION\nSELECT 0 AS Saleable, *\n  FROM @Product\n WHERE NOT ( Obsolete = 'N' OR InStock = 'Y' )\n</code></pre>\n"}, {'answer_id': 40886727, 'author': 'SURJEET SINGH Bisht', 'author_id': 5081921, 'author_profile': 'https://Stackoverflow.com/users/5081921', 'pm_score': 3, 'selected': False, 'text': "<pre><code>  SELECT IIF(Obsolete = 'N' OR InStock = 'Y',1,0) AS Saleable, * FROM Product\n</code></pre>\n"}, {'answer_id': 45578381, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<pre><code>SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 \n             END AS Saleable, * \nFROM Product\n</code></pre>\n"}, {'answer_id': 48541127, 'author': 'Serkan Arslan', 'author_id': 8500110, 'author_profile': 'https://Stackoverflow.com/users/8500110', 'pm_score': 3, 'selected': False, 'text': '<p>As an alternative solution to the <code>CASE</code> statement, a table-driven approach can be used:</p>\n<pre><code>DECLARE @Product TABLE (ID INT, Obsolete VARCHAR(10), InStock VARCHAR(10))\nINSERT INTO @Product VALUES\n(1,\'N\',\'Y\'),\n(2,\'A\',\'B\'),\n(3,\'N\',\'B\'),\n(4,\'A\',\'Y\')\n\nSELECT P.* , ISNULL(Stmt.Saleable,0) Saleable\nFROM\n    @Product P\n    LEFT JOIN\n        ( VALUES\n            ( \'N\', \'Y\', 1 )\n        ) Stmt (Obsolete, InStock, Saleable)\n        ON  P.InStock = Stmt.InStock OR P.Obsolete = Stmt.Obsolete\n</code></pre>\n<p>Result:</p>\n<pre class="lang-none prettyprint-override"><code>ID          Obsolete   InStock    Saleable\n----------- ---------- ---------- -----------\n1           N          Y          1\n2           A          B          0\n3           N          B          1\n4           A          Y          1\n</code></pre>\n'}, {'answer_id': 52696617, 'author': 'laplace', 'author_id': 9822422, 'author_profile': 'https://Stackoverflow.com/users/9822422', 'pm_score': 2, 'selected': False, 'text': "<pre><code>SELECT \n  CAST(\n    CASE WHEN Obsolete = 'N' \n    or InStock = 'Y' THEN ELSE 0 END AS bit\n  ) as Saleable, * \nFROM \n  Product\n</code></pre>\n"}, {'answer_id': 53121744, 'author': 'David Cohn', 'author_id': 10588302, 'author_profile': 'https://Stackoverflow.com/users/10588302', 'pm_score': 3, 'selected': False, 'text': "<p>Question:</p>\n\n<pre><code>SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product\n</code></pre>\n\n<p>ANSI:</p>\n\n<pre><code>Select \n  case when p.Obsolete = 'N' \n  or p.InStock = 'Y' then 1 else 0 end as Saleable, \n  p.* \nFROM \n  Product p;\n</code></pre>\n\n<p>Using aliases -- <code>p</code> in this case -- will help prevent issues.</p>\n"}, {'answer_id': 54932658, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<p>You can have two choices for this to actually implement:</p>\n\n<ol>\n<li><p>Using IIF, which got introduced from SQL Server 2012:</p>\n\n<pre><code>SELECT IIF ( (Obsolete = 'N' OR InStock = 'Y'), 1, 0) AS Saleable, * FROM Product\n</code></pre></li>\n<li><p>Using <code>Select Case</code>:</p>\n\n<pre><code>SELECT CASE\n    WHEN Obsolete = 'N' or InStock = 'Y'\n        THEN 1\n        ELSE 0\n    END as Saleable, *\n    FROM Product\n</code></pre></li>\n</ol>\n"}, {'answer_id': 58643023, 'author': 'Muhammad Awais', 'author_id': 3901944, 'author_profile': 'https://Stackoverflow.com/users/3901944', 'pm_score': 2, 'selected': False, 'text': '<p>It will be something like that:</p>\n\n<pre><code>SELECT OrderID, Quantity,\nCASE\n    WHEN Quantity &gt; 30 THEN "The quantity is greater than 30"\n    WHEN Quantity = 30 THEN "The quantity is 30"\n    ELSE "The quantity is under 30"\nEND AS QuantityText\nFROM OrderDetails;\n</code></pre>\n'}, {'answer_id': 59086460, 'author': 'Salman A', 'author_id': 87015, 'author_profile': 'https://Stackoverflow.com/users/87015', 'pm_score': 2, 'selected': False, 'text': '<p>For the sake of completeness, I would add that SQL uses three-valued logic. The expression:</p>\n\n<pre><code>obsolete = \'N\' OR instock = \'Y\'\n</code></pre>\n\n<p>Could produce three distinct results:</p>\n\n<pre class="lang-none prettyprint-override"><code>| obsolete | instock | saleable |\n|----------|---------|----------|\n| Y        | Y       | true     |\n| Y        | N       | false    |\n| Y        | null    | null     |\n| N        | Y       | true     |\n| N        | N       | true     |\n| N        | null    | true     |\n| null     | Y       | true     |\n| null     | N       | null     |\n| null     | null    | null     |\n</code></pre>\n\n<p>So for example if a product is obsolete but you dont know if product is instock then you dont know if product is saleable. You can write this three-valued logic as follows:</p>\n\n<pre><code>SELECT CASE\n           WHEN obsolete = \'N\' OR instock = \'Y\' THEN \'true\'\n           WHEN NOT (obsolete = \'N\' OR instock = \'Y\') THEN \'false\'\n           ELSE NULL\n       END AS saleable\n</code></pre>\n\n<p>Once you figure out how it works, you can convert three results to two results by deciding the behavior of null. E.g. this would treat null as not saleable:</p>\n\n<pre><code>SELECT CASE\n           WHEN obsolete = \'N\' OR instock = \'Y\' THEN \'true\'\n           ELSE \'false\' -- either false or null\n       END AS saleable\n</code></pre>\n'}, {'answer_id': 60278997, 'author': 'Tharuka Madumal', 'author_id': 12910157, 'author_profile': 'https://Stackoverflow.com/users/12910157', 'pm_score': 3, 'selected': False, 'text': "<p>Using SQL CASE is just like normal If / Else statements.\nIn the below query, if obsolete value = 'N' or if InStock value = 'Y' then the output will be 1. Otherwise the output will be 0.\nThen we put that 0 or 1 value under the Salable Column.</p>\n<pre><code>SELECT\n      CASE\n           WHEN obsolete = 'N' OR InStock = 'Y'\n          THEN 1\n        ELSE 0\n      END AS Salable\n      , *\nFROM PRODUCT\n</code></pre>\n"}, {'answer_id': 61112581, 'author': 'Prashant Marathay', 'author_id': 12046787, 'author_profile': 'https://Stackoverflow.com/users/12046787', 'pm_score': 2, 'selected': False, 'text': '<p>I like the use of the CASE statements, but the question asked for an IF statement in the SQL Select.  What I\'ve used in the past has been:</p>\n<pre><code>SELECT\n\n   if(GENDER = &quot;M&quot;,&quot;Male&quot;,&quot;Female&quot;) as Gender\n\nFROM ...\n</code></pre>\n<p>It\'s like the Excel or sheets IF statements where there is a conditional followed by the true condition and then the false condition:</p>\n<pre><code>if(condition, true, false)\n</code></pre>\n<p>Furthermore, you can nest the <em>if</em> statements (but then use should use a CASE :-)</p>\n<p>(Note: this works in <a href="https://en.wikipedia.org/wiki/MySQL_Workbench" rel="nofollow noreferrer">MySQL Workbench</a>, but it may not work on other platforms)</p>\n'}, {'answer_id': 61941630, 'author': 'The AG', 'author_id': 8692957, 'author_profile': 'https://Stackoverflow.com/users/8692957', 'pm_score': 2, 'selected': False, 'text': "<p>You can use a <em>case</em> statement:</p>\n<pre><code>Select\nCase WHEN (Obsolete = 'N' or InStock = 'Y') THEN 1 ELSE 0 END Saleable,\nProduct.*\nfrom Product\n</code></pre>\n"}, {'answer_id': 65006288, 'author': 'yusuf hayırsever', 'author_id': 10238086, 'author_profile': 'https://Stackoverflow.com/users/10238086', 'pm_score': 3, 'selected': False, 'text': "<pre><code>SELECT\nif((obsolete = 'N' OR instock = 'Y'), 1, 0) AS saleable, *\nFROM\nproduct;\n</code></pre>\n"}, {'answer_id': 71934110, 'author': 'durlove roy', 'author_id': 1549783, 'author_profile': 'https://Stackoverflow.com/users/1549783', 'pm_score': 1, 'selected': False, 'text': "<p>There are multiple conditions.</p>\n<pre><code>SELECT\n\n(CASE\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1001' THEN 'DM'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1002' THEN 'GS'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1003' THEN 'MB'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1004' THEN 'MP'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1005' THEN 'PL'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1008' THEN 'DM-27'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1011' THEN 'PB'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1012' THEN 'UT-2'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1013' THEN 'JGC'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1014' THEN 'SB'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1015' THEN 'IR'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1016' THEN 'UT-3'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1017' THEN 'UT-4'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1019' THEN 'KR'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1020' THEN 'SYB-SB'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1021' THEN 'GR'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1022' THEN 'SYB-KP'\nWHEN RIGHT((LEFT(POSID,5)),4) LIKE '1026' THEN 'BNS'\n\n  ELSE ''\nEND) AS OUTLET\n\nFROM matrixcrm.Transact\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/63447', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6522/']