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