Solved

Help with an SQL select query

Posted on 2009-04-08
8
246 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
8 Comments
 
LVL 39

Accepted Solution

by:
appari earned 400 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 100 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

856 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