Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with an SQL select query

Posted on 2009-04-08
8
Medium Priority
?
256 Views
Last Modified: 2012-05-06
Hi all, I have a table called products (a dummy data version is in the code box below) and I want to change the output data (not the data stored just what comes out from the query) in the column "ProductClass" to one of the following below: (dependent on what is already in that field)

Hats
Bags
Other

Example of the Table:

ID          NAME          PRODUCTCLASS
1          foo               123
2          som              456
3          gee               789
4          hhh               xyz
5          sss               asd
6          ppp               abc
7          bbb               bar


The result should then be (in the output from the Select not updating the DB)

ID          NAME          PRODUCTCLASS
1          foo               Hats
2          som              Hats
3          gee               Hats
4          hhh               Bags
5          sss               Bags
6          ppp               Bags
7          bbb               Other

A dummy if statement is in the code field.

Any help would be greatly appreciated.

Xavier.
if(ProductClass = "abc" or ProductClass = "asd" or ProductClass = "xyz") then
{
   ProductClass = "Bags";
}
else
{
   if(ProductClass = "123" or ProductClass = "456" or ProductClass = "789") then ProductClass = "Hats";
}
else
{
ProductClass = "Other";
}

Open in new window

0
Comment
Question by:drxav
[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
8 Comments
 
LVL 39

Accepted Solution

by:
appari earned 1600 total points
ID: 24104394
Select
ID          , NAME          ,
PRODUCTCLASS,
case when PRODUCTCLASS in ('abc','asd','xyz') then 'bags'
when PRODUCTCLASS in ('123','456','789') then 'Hats'
else 'Other' end from yourtableName
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24104408
Do you mean this:

SELECT ID, NAME,
CASE WHEN PRODUCTCLASS IN ('123', '456', '789') THEN 'Hats'
         WHEN PRODUCTCLASS IN ('xyz', 'asd','abc') THEN 'Bags'
         WHEN PRODUCTCLASS IN ('bar') THEN 'Other'
END AS PRODUCTCLASS
FROM products
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24104415
you can do something like
SELECT ID,NAME,
ProductClass=CASE WHEN ProductClass='abc' or ProductClass='asd'  or ProductClass='xyz' then 'bags' end
from product

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24104417
full query

SELECT ID,NAME,
ProductClass=CASE
WHEN ProductClass='abc' or ProductClass='asd'  or ProductClass='xyz' then 'bags'
WHEN ProductClass='123' or ProductClass='456'  or ProductClass='789' then 'hats'
else 'Other'
end
from product

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24104420
;) bit late in answering!!!
0
 
LVL 4

Author Comment

by:drxav
ID: 24104434
Is there a way to use * rather than specifying the fields? Nb. the query below uses actual objects and field names:

Select TOP 100 *
CASE
WHEN PRODUCTCLASS in ('HATS','IHAT') THEN 'HATS'
WHEN PRODUCTCLASS in ('JACK','IJAC') THEN 'JACK'
ELSE 'Other Shit' END
from dbo.ArTrnDetail
0
 
LVL 4

Author Comment

by:drxav
ID: 24104440
scratch that missing a comma thanks
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24104445
you can do like this.

SELECT top 100 *,
ProductClass=CASE
WHEN ProductClass='abc' or ProductClass='asd'  or ProductClass='xyz' then 'bags'
WHEN ProductClass='123' or ProductClass='456'  or ProductClass='789' then 'hats'
else 'Other'
end
from product


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

604 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