Solved

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

Posted on 2010-11-30
5
529 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 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

13 Experts available now in Live!

Get 1:1 Help Now