Solved

Using a CASE statement in SQL Server 2008 R2

Posted on 2014-09-04
4
273 Views
Last Modified: 2014-09-04
I am trying to construct a CASE statement in my query and not sure how to do. I know how to construct it in a normal condition but I have multiple conditions to check for. I have a column, "profileType" that will contain either a "D", "R" or "".

When "profileType" contains "D", it needs to return "Default. If it contains "R", it needs to return "Recent". If it contains "", just return "".
0
Comment
Question by:BlakeMcKenna
  • 2
4 Comments
 
LVL 16

Expert Comment

by:13598
ID: 40304192
Use a nested IIF statement:

iif(profileType = 'D', 'Default',iif(profileType = 'R', 'Recent',''))
0
 

Author Comment

by:BlakeMcKenna
ID: 40304207
Are you saying insert this statement into the SQL Query? If so, can you be a little more specific in the syntax?

Thanks!
0
 
LVL 1

Accepted Solution

by:
Radha M earned 500 total points
ID: 40304210
Use this. Hope you want to include it in your SELECT clause.
SELECT
   col1,
   col2,
   CASE
      WHEN profileType = 'R'
         THEN 'Recent'
      WHEN profileType = 'D'
         THEN 'Default'
      ELSE ''
   END as col3
FROM
   ---

Open in new window

0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40304221
That worked!

Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now