Open topic with navigation
|COALESCE||Return the first non-
|GREATEST||Returns the largest value from a list of any number of expressions|
|IN||Check whether a value is within a set of values|
|LEAST||Returns the smallest value from a list of any number of expressions|
There are two key uses for the CASE expression:
result[WHEN ...] [ELSE
CASE valueExpression WHEN value1 THEN result1 [WHEN ...] [ELSE result] END
CASE clauses can be used wherever an expression is valid.
condition is an expression that returns a boolean result.
If the result is:
CASEexpression is the result that follows the condition.
WHENclauses are searched in the same manner.
WHEN condition is true then the value of the case expression is the result in the
ELSE clause is omitted and no condition matches, the result is
NuoDB enforces the same type, precision and scale for all the branches of the CASE expression. In situations where the branches do not have the same type, all the branches are coerced to a computed common type. The coercions are enforced during constant folding, which means that when case coercions are applied, a query might fail with a coercion error even if the failing branch would not have been part of the result.
For the generic version of CASE, each predicate in the WHEN clause is type checked in isolation and coercions are applied following the comparison operators model. With the simplified version of the CASE expression, the type of search condition and the types of the WHEN clauses have to be coercible to a common type.
SELECT CASE WHEN P = 1 THEN 'one' WHEN P is null THEN 'null' WHEN P > 10 THEN 'big' ELSE 'unknown' END
SELECT CASE P WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'big' END
Returns the first of its arguments that is not null. Null is returned only if all arguments are null. It is often used to substitute a default value for null values when data is retrieved for display.
COALESCE will not evaluate arguments that are not needed to determine the result (that is, arguments to the right of the first non-null argument are not evaluated).
CREATE TABLE t (a int, b int, c int); INSERT INTO t VALUES (1, 2, 3), (null, 4, 5), (null, null, 6), (null, null, null); SELECT COALESCE(a, b, c, 'something else') FROM t ORDER BY COALESCE(a, b, c, 'something else');-------------- something else 1 4 6
expr2[, ...] )
Returns the largest value from a list of two or more expressions.
If the list of expressions is not all of the same data type, expressions in the list will be converted to the common type computed based on the types of the arguments. An error is returned if a common type does not exist or if not all expressions are convertible to the common type.
NULL if any expression in the list is
SELECT GREATEST('abc', 'ABC') FROM DUAL;greatest --------- abc
SELECT GREATEST('abc', 'ABC', NULL) FROM DUAL;greatest --------- <null> /* Count teams that have more LOSSES than WINS or TIES */
SELECT COUNT(*) FROM hockey.teams WHERE GREATEST(wins, losses, ties) = losses;COUNT ------ 685
SELECT GREATEST(1,2,'abc') FROM DUAL;unable to convert string "abc" into type "number"
expr1 is not
expr1; otherwise it returns
expr2. The returned type of
IFNULL is the common type of the two arguments. Both arguments must be coercible to the common type.
SELECT IFNULL(1,0) FROM dual;- 1
SELECT IFNULL(NULL,10) FROM dual;-- 10
SELECT 1.0000/10.0000, IFNULL(1.000/null,1.000/10.0000), IFNULL(1.000/null,10.0000), IFNULL(null/10.000,1.00) FROM dual;IFNULL IFNULL IFNULL IFNULL ----- ----- ------- ------ .1000 .1000 10.0000 1.0000
which is equivalent to:
x3[OR ... ] )
Checks if the specified
column data is a member of the specified list. It can be used with
CHECK, and creation of views.
The IN operator applies the comparison common type algorithm in two steps, first on the elements in the IN list and then by computing the common type between the search argument and the common type of the IN list. The common type on the list of arguments on the right hand side is computed from left to right by computing the common type between the currently computed common type and the next argument. This makes the IN list not commutative in terms of the types of the arguments.
SELECT * FROM hockey WHERE position IN ('Fan','Goalie');ID NUMBER NAME POSITION TEAM --- ------- -------------- --------- ------ 22 35 ANTON KHUDOBIN Goalie Bruins 23 40 TUUKKA RASK Goalie Bruins 24 1 MAX SUMMIT Fan Bruins
Returns the smallest value from a list of two or more expressions.
If the list of expressions is not all the same data type, NuoDB applies the same type rules as for the GREATEST expression.
NULL if any expression in the list is
SELECT LEAST('abc', 'ABC') FROM DUAL;least ------ ABC
SELECT LEAST('abc', 'ABC', NULL) FROM DUAL;least ------ <null> /* Count teams that have more LOSSES and TIES than WINS */
SELECT COUNT(*) FROM hockey.teams WHERE LEAST(wins, losses, ties) = wins;COUNT ------ 20
SELECT LEAST(1,2,'abc') FROM DUAL;unable to convert string "abc" into type "number"
Returns a null value if
value2 are equal; otherwise it returns
An error is returned if
value2do not have the same type and cannot be coerced to a common type.
CREATE TABLE t (a int, b int); INSERT INTO t VALUES (null, null), (null, 1), (1, null), (1, 1); SQL>SELECT COALESCE (a, 0) as a, COALESCE (b, 0) as b, COALESCE (nullif (a, b), 0) AS nullif FROM t;A B NULLIF ---- ---- ------- 0 0 0 0 1 0 1 0 1 1 1 0