2018-11-20 19:43:05 -05:00
|
|
|
[role="xpack"]
|
|
|
|
[testenv="basic"]
|
|
|
|
[[sql-functions-conditional]]
|
|
|
|
=== Conditional Functions
|
|
|
|
|
|
|
|
Functions that return one of their arguments by evaluating in an if-else manner.
|
|
|
|
|
|
|
|
[[sql-functions-conditional-coalesce]]
|
|
|
|
==== `COALESCE`
|
|
|
|
|
2018-12-21 16:25:54 -05:00
|
|
|
.Synopsis:
|
2018-11-20 19:43:05 -05:00
|
|
|
[source, sql]
|
|
|
|
----
|
2018-12-21 16:25:54 -05:00
|
|
|
COALESCE(expression<1>, expression<2>, ...)
|
2018-11-20 19:43:05 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
*Input*:
|
|
|
|
|
|
|
|
<1> 1st expression
|
|
|
|
|
|
|
|
<2> 2nd expression
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
**N**th expression
|
|
|
|
|
|
|
|
COALESCE can take an arbitrary number of arguments.
|
|
|
|
|
|
|
|
*Output*: one of the expressions or `null`
|
|
|
|
|
|
|
|
.Description
|
|
|
|
|
|
|
|
Returns the first of its arguments that is not null.
|
|
|
|
If all arguments are null, then it returns `null`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNonNull]
|
2018-11-20 19:43:05 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNull]
|
2018-11-20 19:43:05 -05:00
|
|
|
----
|
2018-11-21 11:07:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
[[sql-functions-conditional-ifnull]]
|
|
|
|
==== `IFNULL`
|
|
|
|
|
2018-12-21 16:25:54 -05:00
|
|
|
.Synopsis:
|
2018-11-21 11:07:07 -05:00
|
|
|
[source, sql]
|
|
|
|
----
|
2018-12-21 16:25:54 -05:00
|
|
|
IFNULL(expression<1>, expression<2>)
|
2018-11-21 11:07:07 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
*Input*:
|
|
|
|
|
|
|
|
<1> 1st expression
|
|
|
|
|
|
|
|
<2> 2nd expression
|
|
|
|
|
|
|
|
|
|
|
|
*Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
|
|
|
|
|
|
|
|
.Description
|
|
|
|
|
|
|
|
Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
|
|
|
|
Returns the first of its arguments that is not null.
|
|
|
|
If all arguments are null, then it returns `null`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnFirst]
|
2018-11-21 11:07:07 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnSecond]
|
2018-11-21 11:07:07 -05:00
|
|
|
----
|
2018-11-21 17:15:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
[[sql-functions-conditional-isnull]]
|
|
|
|
==== `ISNULL`
|
|
|
|
|
2018-12-21 16:25:54 -05:00
|
|
|
.Synopsis:
|
2018-11-21 17:15:10 -05:00
|
|
|
[source, sql]
|
|
|
|
----
|
2018-12-21 16:25:54 -05:00
|
|
|
ISNULL(expression<1>, expression<2>)
|
2018-11-21 17:15:10 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
*Input*:
|
|
|
|
|
|
|
|
<1> 1st expression
|
|
|
|
|
|
|
|
<2> 2nd expression
|
|
|
|
|
|
|
|
|
|
|
|
*Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
|
|
|
|
|
|
|
|
.Description
|
|
|
|
|
|
|
|
Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
|
|
|
|
Returns the first of its arguments that is not null.
|
|
|
|
If all arguments are null, then it returns `null`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnFirst]
|
2018-11-21 17:15:10 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnSecond]
|
2018-11-21 17:15:10 -05:00
|
|
|
----
|
2018-11-22 05:41:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
[[sql-functions-conditional-nvl]]
|
|
|
|
==== `NVL`
|
|
|
|
|
2018-12-21 16:25:54 -05:00
|
|
|
.Synopsis:
|
2018-11-22 05:41:00 -05:00
|
|
|
[source, sql]
|
|
|
|
----
|
2018-12-21 16:25:54 -05:00
|
|
|
NVL(expression<1>, expression<2>)
|
2018-11-22 05:41:00 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
*Input*:
|
|
|
|
|
|
|
|
<1> 1st expression
|
|
|
|
|
|
|
|
<2> 2nd expression
|
|
|
|
|
|
|
|
|
|
|
|
*Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
|
|
|
|
|
|
|
|
.Description
|
|
|
|
|
|
|
|
Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
|
|
|
|
Returns the first of its arguments that is not null.
|
|
|
|
If all arguments are null, then it returns `null`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnFirst]
|
2018-11-22 05:41:00 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnSecond]
|
2018-11-22 05:41:00 -05:00
|
|
|
----
|
2018-11-23 16:19:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
[[sql-functions-conditional-nullif]]
|
|
|
|
==== `NULLIF`
|
|
|
|
|
2018-12-21 16:25:54 -05:00
|
|
|
.Synopsis:
|
2018-11-23 16:19:27 -05:00
|
|
|
[source, sql]
|
|
|
|
----
|
2018-12-21 16:25:54 -05:00
|
|
|
NULLIF(expression<1>, expression<2>)
|
2018-11-23 16:19:27 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
*Input*:
|
|
|
|
|
|
|
|
<1> 1st expression
|
|
|
|
|
|
|
|
<2> 2nd expression
|
|
|
|
|
|
|
|
|
|
|
|
*Output*: `null` if the 2 expressions are equal, otherwise the 1st expression.
|
|
|
|
|
|
|
|
.Description
|
|
|
|
|
|
|
|
Returns `null` when the two input expressions are equal and
|
|
|
|
if not, it returns the 1st expression.
|
|
|
|
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnFirst]
|
2018-11-23 16:19:27 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnNull]
|
2018-11-23 16:19:27 -05:00
|
|
|
----
|
2018-11-26 12:21:36 -05:00
|
|
|
|
|
|
|
|
|
|
|
[[sql-functions-conditional-greatest]]
|
|
|
|
==== `GREATEST`
|
|
|
|
|
2018-12-21 16:25:54 -05:00
|
|
|
.Synopsis:
|
2018-11-26 12:21:36 -05:00
|
|
|
[source, sql]
|
|
|
|
----
|
2018-12-21 16:25:54 -05:00
|
|
|
GREATEST(expression<1>, expression<2>, ...)
|
2018-11-26 12:21:36 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
*Input*:
|
|
|
|
|
|
|
|
<1> 1st expression
|
|
|
|
|
|
|
|
<2> 2nd expression
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
**N**th expression
|
|
|
|
|
|
|
|
GREATEST can take an arbitrary number of arguments and
|
|
|
|
all of them must be of the same data type.
|
|
|
|
|
|
|
|
*Output*: one of the expressions or `null`
|
|
|
|
|
|
|
|
.Description
|
|
|
|
|
|
|
|
Returns the argument that has the largest value which is not null.
|
|
|
|
If all arguments are null, then it returns `null`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNonNull]
|
2018-11-26 12:21:36 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNull]
|
2018-11-26 12:21:36 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
|
|
|
|
[[sql-functions-conditional-least]]
|
|
|
|
==== `LEAST`
|
|
|
|
|
2018-12-21 16:25:54 -05:00
|
|
|
.Synopsis:
|
2018-11-26 12:21:36 -05:00
|
|
|
[source, sql]
|
|
|
|
----
|
2018-12-21 16:25:54 -05:00
|
|
|
LEAST(expression<1>, expression<2>, ...)
|
2018-11-26 12:21:36 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
*Input*:
|
|
|
|
|
|
|
|
<1> 1st expression
|
|
|
|
|
|
|
|
<2> 2nd expression
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
**N**th expression
|
|
|
|
|
|
|
|
LEAST can take an arbitrary number of arguments and
|
|
|
|
all of them must be of the same data type.
|
|
|
|
|
|
|
|
*Output*: one of the expressions or `null`
|
|
|
|
|
|
|
|
.Description
|
|
|
|
|
|
|
|
Returns the argument that has the smallest value which is not null.
|
|
|
|
If all arguments are null, then it returns `null`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNonNull]
|
2018-11-26 12:21:36 -05:00
|
|
|
----
|
|
|
|
|
|
|
|
["source","sql",subs="attributes,callouts,macros"]
|
|
|
|
----
|
2019-03-25 09:22:59 -04:00
|
|
|
include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNull]
|
2018-11-26 12:21:36 -05:00
|
|
|
----
|