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
 SQL Server Development (2000)
 Help with SQL Statement

Author  Topic 

RexHelios
Starting Member

3 Posts

Posted - 2008-12-09 : 13:21:56
Hi,

I have a table with the following structure and data

State Price
----- -----
NULL $100
CA $120
TX $75
IA $60

I have one of the 50 states in a variable. Now I need to extract the corresponding price from the table above. Whenever there is match for the state, I would select the corresponding price. But if there is no match found, I should get the generic price where State is NULL.

How can I write a SQL Statement/T-SQL block to achieve this? Any help or inputs will be grately appreciated.

Regards,
RexHelios

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-09 : 13:28:20
[code]SELECT TOP 1 *
FROM Table
WHERE State=@State
OR State IS NULL
ORDER BY COALESCE(State,'ZZZZZZZ') ASC
[/code]
Go to Top of Page

RexHelios
Starting Member

3 Posts

Posted - 2008-12-09 : 14:30:49
Thank you very much, Visakh16. It worked like a charm!

Regards,
RexHelios
Go to Top of Page
   

- Advertisement -