How can I write an IF...THEN in an SQL SELECT?
How can I write an IF...THEN in an SQL SELECT?
270
11-Jul-2023
Updated on 12-Jul-2023
Aryan Kumar
12-Jul-2023You can write an
if thenin an SQL select by using theCASEstatement. TheCASEstatement allows you to execute different code depending on the value of a condition.The syntax for writing an
if thenin an SQL select is as follows:SQL
The
column1,column2, etc. are the columns that you want to select. Thecondition1,condition2, etc. are the conditions that you want to check. Thevalue1,value2, etc. are the values that you want to return if the condition is met. Theif_then_columnis the name of the new column that will be created.For example, the following code will select the
CustomerNamecolumn and theCountrycolumn from theCustomerstable. If theCountrycolumn isUSA, then theif_then_columnwill be set toUS Customer. Otherwise, theif_then_columnwill be set toNon-US Customer.SQL