conditional.asciidoc 5.0 KB

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