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

x
?
Solved

How to use case in MSSQL with a list of values?

Posted on 2010-11-30
5
Medium Priority
?
543 Views
Last Modified: 2012-05-10
I have a query where I need to assign a value depending on another set of values. For example...

column1
2
2
3
4
10
5

When rows in column 1 are equal to 2 or 3, I would like a column to be equal to "WT". All others should be "SP"... I wrote this:
case column1 when (2,3) then "WT" else "SP". Got a few syntax errors... Is this the correct way to write a CASE statement?
0
Comment
Question by:horalia
5 Comments
 
LVL 3

Expert Comment

by:alexbumbacea
ID: 34240787
SELECT case(column1)
when 1 then 'nt'
else 'unknown'

FROM table
0
 
LVL 3

Expert Comment

by:alexbumbacea
ID: 34240806
SELECT case(column1)
when column2 then 'wt'
when column3 then 'wt'
else 'sp'

FROM table

Sorry for previous. I haven't read the entire text.
0
 
LVL 6

Accepted Solution

by:
hyphenpipe earned 2000 total points
ID: 34240834
select case when column_1 in (2,3) then 'wt' else 'sp' end
from table
0
 
LVL 5

Expert Comment

by:Vipul Patel
ID: 34240861
Below sample might be resolve your doubts;

DECLARE @Value INT=5

SELECT
CASE
WHEN @Value IN (2,3) THEN 'Vips'
WHEN @Value IN (6,7) THEN 'Patel'
ELSE 'BLANK'
END

please see attached modified code.

Andd visit below link for more information
http://msdn.microsoft.com/en-us/library/ms181765.aspx
SELECT 
CASE 
WHEN column1 IN (2,3) THEN 'WT'
ELSE 'SP'
END

Open in new window

0
 

Author Closing Comment

by:horalia
ID: 34241523
Exactly what I needed, thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

927 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