Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Simple SQL help for a newbie... very newbie

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-11-18 : 08:33:01
Sharon writes "I am new to SQL and I am trying to filter information for a products web page I am building. Here is the SQL statement I have at the moment which is totally wrong...

SELECT Prod_ID, Prod_Name, Prod_Price, Prod_Image
FROM Product_Item
WHERE Prod_Price = 'SmallPrice'
IF 'SmallPrice' IS NOT NULL
THEN 'SmallPrice'
ELSE 'MediumPrice' IS NOT NULL
THEN 'MediumPrice' END IF
ORDER BY Prod_Name ASC

I am trying to show all products on the page with the product name, price and image. However, I want to filter the price information because each product has several different prices depending on it's size. The way I want to filter it is by displaying the small price,(next to the word from:), and if the product doesn't come in small, then I want to display the medium price. Does this make sense? I hope so...

Can someone help me out. I'm sure this is simple for someone on the SQL team! :)

Thank you!
Sincerely,
Sharon"

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-18 : 08:52:42
In this case you might use the COALESCE function

SELECT COALESCE(SmallPrice,MediumPrice) AS Prod_Price FROM Product_Item

This will take the first NON NULL value of SmallPrice,MediumPrice.

For conditional programming in SQL Server you can use the CASE statement:
CASE WHEN <condition1> THEN <dothis> WHEN <condition2> THEN <dothat> ELSE <dozip> END

rockmoose
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-11-18 : 09:13:04
Take a look:

http://weblogs.sqlteam.com/jeffs/archive/2003/11/14/513.aspx

- Jeff
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-11-18 : 09:29:05
quote:
Simple SQL help for a newbie... very newbie


I read your blog Jeff, very nice.
I would call that stuff, advanced ?

rockmoose
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-11-18 : 11:10:10
i guess it depends on your background; if you are good with logic and boolean expressions and programming in general, but new at SQL, it should be helpful; if you are new to computers/programming in general and especially logic, then it may be too complicated.

but the overall logical concept of:

IF A THEN B (or "A -> B" or "A implies B")

being equivalent to

(Not A) or B

is a very handy thing to know !

- Jeff
Go to Top of Page
   

- Advertisement -