Case statement in Where Clause – SQL Server – Conditional Where clause

Using Case statement in Where Clause in SQl Server. This script is used to create the conditional where clause

In few scenario, we might need to perform conditional where clause. Some time the condition can be written well in OR with Where clause but in some scenario it is not possible.

so this time, i have come up with one more article on writing the conditional where clause.


DECLARE @Student Table (Stream Varchar(20), [Name] Varchar(20), isAllow BIT )

INSERT Into @Student
('Computer Engineering','Minal Zaa', 0)

INSERT Into @Student
('Civil Engineering','Manoranjan Sahoo', 1)

INSERT Into @Student
('scientist','Santosh Karemore', 1)

INSERT Into @Student
('BCom','Rahul jha', 0)

    WHEN Stream = 'Computer Engineering' THEN 1
    WHEN isAllow = 1 THEN 1
END = 1


Case statement in Where Clause

In above code, we have checked that if stream is ‘Computer Engineering’ then display the record or if IsAllow bit is true then display the record. Same query can be written using OR operator but here i wanted to demonstrate the use of Case statement in Where Clause.

Related posts

3 thoughts on “Case statement in Where Clause – SQL Server – Conditional Where clause”

  1. i want write where clause

    when Address line = address line two then ‘Y’
    when name = address_line_one then ‘Y’

    How can i fetch this in case statement.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.