Solved

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

Posted on 2008-10-09
2
6,874 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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