Solved

Help with an SQL select query

Posted on 2009-04-08
8
239 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

757 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

23 Experts available now in Live!

Get 1:1 Help Now