?
Solved

Help with an SQL select query

Posted on 2009-04-08
8
Medium Priority
?
255 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

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 part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

765 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