conditional.asciidoc 4.9 KB

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