In SQL Server, there are few conditional statements like IF and IF-ELSE in SQL Server or Switch case in SQL Server, but in this article, I have mentioned IF-ELSE Conditional statements.

SQL IF statements allow you to implement conditions for executing different code blocks in your SQL scripts, if you have learned any programming languages like Javascript, C#, Java, C, etc then you must be aware of Conditional statements, in SQL Server also IF and IF-Else works the same way.

IF Statement in SQL Server

The syntax of the SQL IF Statement is simple.

You use the 'IF' clause to implement the SQL IF statement as shown in the following code snippet:

IF boolean_condition is True
// Scripts to execute, if condition is true

For example, we have below IF condition

DECLARE @Age INT
SET @Age = 11

IF @Age > 10
  PRINT 'Age is greater than 10'

Output:

Age is greater than 10

if-condition-sql-server

In the above example, since the IF condition is true (Age > 10), then it is executing SQL Statement inside IF, otherwise it wouldn't have executed any code.

If you want to execute multiple statements inside an IF statement, you need to enclose the multiple code lines inside the BEGIN and END clauses, as shown below:

DECLARE @Age INT
SET @Age = 11

IF @Age > 10
BEGIN
  PRINT 'Age is greater than 10'
  PRINT 'Age is '+ CONVERT(varchar(10), @Age)
END

Output:

Age is greater than 10
Age is 11

IF-ELSE Statement in SQL Server

The SQL Server IF statement is often used with ELSE statement.

You can use the ELSE block if you want to execute an SQL script when the IF condition returns false.

Syntax for using If-Else in SQL Server

IF Boolean_expression   
     { sql_statement | statement_block }   
ELSE   
     { sql_statement | statement_block } 

If we consider above IF statement example, then we can convert it in IF-ELSE, it would look like below

DECLARE @Age INT
SET @Age = 6

IF @Age > 10
BEGIN
  PRINT 'Age is greater than 10'
  PRINT 'Age is '+ CONVERT(varchar(10), @Age)
END
ELSE
 PRINT 'Age is less than 10'

Output:

Age is less than 10

in the above example, since the IF condition is not true, so ELSE block SQL statements are executed.

if-else-statement-sql-server

Nested IF-Else Statement in SQL Server

In the above examples, we saw simple IF or IF-ELSE statement examples, but we can also add nested IF-ELSE conditions in SQL Server

DECLARE @Age INT
SET @Age = 6

IF @Age > 100  
   PRINT 'Very old age.'
ELSE   
   BEGIN  
      IF @Age < 10  
      PRINT 'Age is low.'
   ELSE  
      PRINT 'Age is medium.'
   END 
GO  

The output of the above SQL Query would be as below

Age is low.

Since Age = 6, so first IF condition is not true, and ELSE is executed, but IF inside ELSE is executed.

You may also like to read:

Understanding SQL server switch case (With Example)

Find Duplicate Rows in SQL Server

SQL server date format and converting it (Various examples)

How to convert nvarchar column to datetime in SQL Server?

What is SQL Query to get dates between two dates?

How to check if Columns exits in SQL Server table?

What is database management system and it's types

Error "Connection was established with server but error occurred during login process" SSMS