conditional.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-functions-conditional]]
  4. === Conditional Functions
  5. Functions that return one of their arguments by evaluating in an if-else manner.
  6. [[sql-functions-conditional-coalesce]]
  7. ==== `COALESCE`
  8. .Synopsis:
  9. [source, sql]
  10. ----
  11. COALESCE(
  12. expression, <1>
  13. expression, <2>
  14. ...)
  15. ----
  16. *Input*:
  17. <1> 1st expression
  18. <2> 2nd expression
  19. ...
  20. **N**th expression
  21. COALESCE can take an arbitrary number of arguments.
  22. *Output*: one of the expressions or `null`
  23. .Description
  24. Returns the first of its arguments that is not null.
  25. If all arguments are null, then it returns `null`.
  26. ["source","sql",subs="attributes,callouts,macros"]
  27. ----
  28. include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNonNull]
  29. ----
  30. ["source","sql",subs="attributes,callouts,macros"]
  31. ----
  32. include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNull]
  33. ----
  34. [[sql-functions-conditional-greatest]]
  35. ==== `GREATEST`
  36. .Synopsis:
  37. [source, sql]
  38. ----
  39. GREATEST(
  40. expression, <1>
  41. expression, <2>
  42. ...)
  43. ----
  44. *Input*:
  45. <1> 1st expression
  46. <2> 2nd expression
  47. ...
  48. **N**th expression
  49. GREATEST can take an arbitrary number of arguments and
  50. all of them must be of the same data type.
  51. *Output*: one of the expressions or `null`
  52. .Description
  53. Returns the argument that has the largest value which is not null.
  54. If all arguments are null, then it returns `null`.
  55. ["source","sql",subs="attributes,callouts,macros"]
  56. ----
  57. include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNonNull]
  58. ----
  59. ["source","sql",subs="attributes,callouts,macros"]
  60. ----
  61. include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNull]
  62. ----
  63. [[sql-functions-conditional-ifnull]]
  64. ==== `IFNULL`
  65. .Synopsis:
  66. [source, sql]
  67. ----
  68. IFNULL(
  69. expression, <1>
  70. expression) <2>
  71. ----
  72. *Input*:
  73. <1> 1st expression
  74. <2> 2nd expression
  75. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  76. .Description
  77. Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  78. Returns the first of its arguments that is not null.
  79. If all arguments are null, then it returns `null`.
  80. ["source","sql",subs="attributes,callouts,macros"]
  81. ----
  82. include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnFirst]
  83. ----
  84. ["source","sql",subs="attributes,callouts,macros"]
  85. ----
  86. include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnSecond]
  87. ----
  88. [[sql-functions-conditional-isnull]]
  89. ==== `ISNULL`
  90. .Synopsis:
  91. [source, sql]
  92. ----
  93. ISNULL(
  94. expression, <1>
  95. expression) <2>
  96. ----
  97. *Input*:
  98. <1> 1st expression
  99. <2> 2nd expression
  100. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  101. .Description
  102. Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  103. Returns the first of its arguments that is not null.
  104. If all arguments are null, then it returns `null`.
  105. ["source","sql",subs="attributes,callouts,macros"]
  106. ----
  107. include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnFirst]
  108. ----
  109. ["source","sql",subs="attributes,callouts,macros"]
  110. ----
  111. include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnSecond]
  112. ----
  113. [[sql-functions-conditional-least]]
  114. ==== `LEAST`
  115. .Synopsis:
  116. [source, sql]
  117. ----
  118. LEAST(
  119. expression, <1>
  120. expression, <2>
  121. ...)
  122. ----
  123. *Input*:
  124. <1> 1st expression
  125. <2> 2nd expression
  126. ...
  127. **N**th expression
  128. LEAST can take an arbitrary number of arguments and
  129. all of them must be of the same data type.
  130. *Output*: one of the expressions or `null`
  131. .Description
  132. Returns the argument that has the smallest value which is not null.
  133. If all arguments are null, then it returns `null`.
  134. ["source","sql",subs="attributes,callouts,macros"]
  135. ----
  136. include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNonNull]
  137. ----
  138. ["source","sql",subs="attributes,callouts,macros"]
  139. ----
  140. include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNull]
  141. ----
  142. [[sql-functions-conditional-nullif]]
  143. ==== `NULLIF`
  144. .Synopsis:
  145. [source, sql]
  146. ----
  147. NULLIF(
  148. expression, <1>
  149. expression) <2>
  150. ----
  151. *Input*:
  152. <1> 1st expression
  153. <2> 2nd expression
  154. *Output*: `null` if the 2 expressions are equal, otherwise the 1st expression.
  155. .Description
  156. Returns `null` when the two input expressions are equal and
  157. if not, it returns the 1st expression.
  158. ["source","sql",subs="attributes,callouts,macros"]
  159. ----
  160. include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnFirst]
  161. ----
  162. ["source","sql",subs="attributes,callouts,macros"]
  163. ----
  164. include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnNull]
  165. ----
  166. [[sql-functions-conditional-nvl]]
  167. ==== `NVL`
  168. .Synopsis:
  169. [source, sql]
  170. ----
  171. NVL(
  172. expression, <1>
  173. expression) <2>
  174. ----
  175. *Input*:
  176. <1> 1st expression
  177. <2> 2nd expression
  178. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  179. .Description
  180. Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  181. Returns the first of its arguments that is not null.
  182. If all arguments are null, then it returns `null`.
  183. ["source","sql",subs="attributes,callouts,macros"]
  184. ----
  185. include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnFirst]
  186. ----
  187. ["source","sql",subs="attributes,callouts,macros"]
  188. ----
  189. include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnSecond]
  190. ----