?
Solved

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

Posted on 2008-10-09
2
Medium Priority
?
7,096 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 2000 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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…

770 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