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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And SQL Server syntaxas a bonus:
SELECT CASE WHEN a = 0 THEN 100 ELSE a END + b AS sum FROM myTable WHERE 1=1
SELECT CASE WHEN a = 0 THEN 100 ELSE a END + b AS sum FROM myTable WHERE 1=1
ASKER