Solved

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

Posted on 2010-11-30
5
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

617 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