Solved

How to do an if/else in an Informix SQL Query

Posted on 2008-10-09
2
6,320 Views
Last Modified: 2013-12-24
I am doing a simple select, but I am not sure of the syntax for an if/else statement in informix.
I want the mobile number to be from A2.phone but if A2.phone is null and A4.phone is not null then I want A4.phone to also be the mobile number.

Here is the select:
select I.id,
        pzfirstname(I.fullname) fname,
        pzlastname(I.fullname) lname,
        A.phone office,
        A2.phone mobile,
        A3.line1 || A3.line2 email,
        A4.phone mobile2,
        "Staff" contact_type
from id_rec I
0
Comment
Question by:seahawk9
2 Comments
 
LVL 5

Accepted Solution

by:
flutophilus earned 500 total points
ID: 22685442
You can't do an IF/ELSE in informix sql, you must write a stored procedure.
Here's an example of an IF/ELSE from the documentation that compares two strings that are passed in
CREATE PROCEDURE str_compare (str1 CHAR(20), str2 CHAR(20))
RETURNING INT;
DEFINE result INT;
IF str1 > str2 then
result =1;
ELIF str2 > str1 THEN
result = -1;
ELSE
result = 0;
END IF
RETURN result;
END PROCEDURE -- str_compare

You can create a stored procedure by using dbaccess - you can't create a stored procedure using isql.
You can run a stored procedure by using the EXECUTE PROCEDURE .. INTO ... syntax.
Check out the Informix Guide to SQL : Reference and Informix Guide to SQL : Syntax, available free online via your favourite search engine.
0
 

Expert Comment

by:Geoffers99
ID: 22913102
Rather than a stored procedure you could simply use a CASE expression.

select I.id,
        pzfirstname(I.fullname) fname,
        pzlastname(I.fullname) lname,
        A.phone office,
        CASE
        WHEN A2.phone is null
              THEN A4.phone
         ELSE
               A2.phone
         END mobile,
        A3.line1 || A3.line2 email,
        A4.phone mobile2,
        "Staff" contact_type
from id_rec I
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now