Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using a CASE statement in SQL Server 2008 R2

Posted on 2014-09-04
4
Medium Priority
?
303 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 2000 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

772 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