SQL-J Language Reference
Page 66 of 121

Conditional (?:)

You can place a conditional expression anywhere an expression is allowed. It chooses an expression to evaluate based on a boolean test.

Syntax

BooleanExpression ? ThenExpression : ElseExpression

ThenExpression and ElseExpression are both expressions that must be type-compatible. For built-in types, this means that the types must be the same or a built-in broadening conversion must exist between the types. For Java data types, this means that the types must be the same or one of the types must be assignable to the other one due to a super/subclass or interface/implementor relationship.

You do not need to use the conditional expression for avoiding NullPointerExceptions when a nullable column becomes a method receiver. If the value of the instance specified in an instance method invocation is null, the result of the invocation is null (SQL-J NULL). However, you still may need to use the conditional expression for when a nullable column is a primitive method parameter; the conditional expression is a good way of doing that.

Conditional (?:) Example

--if there's no matching city_id (null), display a 0
SELECT country, city_id IS NULL? 0 : city_id
FROM Countries LEFT OUTER JOIN Cities
USING (country_ISO_code)

-- If business_seats_taken has any nulls, you'll get an exception
SELECT NEW java.lang.Integer(business_seats_taken)
FROM flightavailability

-- using the conditional expression, you can "convert" any
-- nulls into 0, a valid paramter
SELECT NEW java.lang.Integer(
    (business_seats_taken IS NULL) ? 0: business_seats_taken) FROM flightavailability