Showing posts with label cotherwise. Show all posts
Showing posts with label cotherwise. Show all posts

Sunday, February 19, 2012

Implied If Statements

What would be the best function to do an implied if statement in SQL?
ex.
if column A = 2
then Column B/Column C
otherwise Column B
Thanks for your help again!!Use a CASE expression:
CASE ColA
WHEN 2 THEN ColB / ColC
ELSE ColB
END
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"Barce5" <Barce5@.discussions.microsoft.com> wrote in message
news:18BA472F-EF66-42FE-A0BC-C19C201642AF@.microsoft.com...
> What would be the best function to do an implied if statement in SQL?
> ex.
> if column A = 2
> then Column B/Column C
> otherwise Column B
> Thanks for your help again!!|||Use a CASE expression.
Example:
select case when colA = 2 then colB/nullif(colC, 0) else colB end
from yourTable
go
AMB
"Barce5" wrote:

> What would be the best function to do an implied if statement in SQL?
> ex.
> if column A = 2
> then Column B/Column C
> otherwise Column B
> Thanks for your help again!!