Nested IF in TSQL

Data SenseiData Sensei
1 min read
--nesting example
DECLARE @Var1 INT = 15
IF @Var1 % 5 = 0 BEGIN
    IF @Var1 % 3 = 0 BEGIN 
        print 'the number is divisible by 3 and 5'
    END
    ELSE BEGIN 
        print 'the number is divisible by 5'
    END
END
ELSE BEGIN 
    IF @Var1 % 3 = 0 BEGIN 
        print 'the number is divisible by 3'
    END
    ELSE BEGIN 
        print 'the number is not divisible by 3 or 5'
    END
END

Structure

IF --condition
BEGIN
--operation if true 
END

ELSE
BEGIN
--operation if false
END
0
Subscribe to my newsletter

Read articles from Data Sensei directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Data Sensei
Data Sensei

A data analytics engineer with four years of experience working as a data engineer. Holds a MSc in Data.