How do I use a case statement in the where clause in SQL Server?

Vaishali
1 min readMar 9, 2020

--

CASE STATEMENT IN WHERE CLAUSE:

  • The CASE statement returns the value based on condition.
  • We can use a case statement in Where, Order by and Group by clause.
  • In the Customer table, I have displayed the First Name is Ram or the Last Name is Sharma’s salary.
  • So, by using a CASE statement with the where condition displays the result.

EXAMPLE:-

  • I want to display Salary in the Customer table the First Name is ‘RAM’ or Last Name is ‘SHARMA’
CUSTOMER TABLE

QUERY:-

SELECT *

FROM [dbo].[Customer]

WHERE [Salary] =

CASE

WHEN [First_Name] = ‘RAM’ THEN [Salary]

WHEN [Last_Name] = ‘SHARMA’ THEN [Salary]

ELSE NULL

END

RESULT:-

RESULT

If you are new to SQL Server start with the following must-watch video:-

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (5)

Write a response