Link to home
Start Free TrialLog in
Avatar of nlhess2003
nlhess2003

asked on

IF statement in sql query?

Say I have the query:
SELECT (a + b) AS sum FROM myTable WHERE 1

I want to do something like:
SELECT ((IF a = 0 RETURN 100 ELSE RETURN a END IF) + b) AS sum FROM myTable WHERE 1
but that obviously doesn't work. I just want to add a new value to b that i want if a = 0.

I need to do this in mysql so i can sort results.
Is there an easy way to do this?
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nlhess2003
nlhess2003

ASKER

Yes! Thank you! I knew it was simple.
And SQL Server syntaxas a bonus:

SELECT CASE WHEN a = 0 THEN 100 ELSE a END + b AS sum FROM myTable WHERE 1=1