Solved

Using a CASE statement in SQL Server 2008 R2

Posted on 2014-09-04
4
278 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

16 Experts available now in Live!

Get 1:1 Help Now