Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using IFs in Select Statements

Status
Not open for further replies.

joebickley

Programmer
Aug 28, 2001
139
GB
I have a table with fields AA BB and CC

My select statement multiplies AA by CC to give my results. However if there is a value in BB i want to use that instead of AA in the sum.

Cant work out how to do it, anyone help?

Thanks


 
You can't use IF's in this way, what's need is a CASE statment. Though this can also be done with an ISNULL function depending on your data.

If you have null's in BB where there is no value then use IsNull.
Select IsNull( BB, AA) *CC From tbl

However if you have 0 in BB where there is no value you will need to use something like this.
Select Case BB When 0 Then AA * CC Else BB * CC End From tbl

 
Fan Bloody tastic.

BB is always null unless its been updated and if there is a 0 in it should be used

Thanks a lot

 

Then the SQL should be:


Select Case when BB is null Then AA * CC Else BB * CC End From tbl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top