conditional.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-functions-conditional]]
  4. === Conditional Functions And Expressions
  5. Functions that return one of their arguments by evaluating in an if-else manner.
  6. [[sql-functions-conditional-case]]
  7. ==== `CASE`
  8. .Synopsis:
  9. [source, sql]
  10. ----
  11. CASE WHEN condition THEN result
  12. [WHEN ...]
  13. [ELSE default_result]
  14. END
  15. ----
  16. *Input*:
  17. One or multiple _WHEN *condition* THEN *result_* clauses are used and the expression can optionally have
  18. an _ELSE *default_result_* clause. Every *condition* should be a boolean expression.
  19. *Output*: one of the *result* expressions if the corresponding _WHEN *condition_* evaluates to `true` or
  20. the *default_result* if all _WHEN *condition_* clauses evaluate to `false`. If the optional _ELSE *default_result_*
  21. clause is missing and all _WHEN *condition_* clauses evaluate to `false` then `null` is returned.
  22. .Description
  23. The CASE expression is a generic conditional expression which simulates if/else statements of other programming languages
  24. If the condition’s result is true, the value of the result expression that follows the condition will be the returned
  25. the subsequent when clauses will be skipped and not processed.
  26. ["source","sql",subs="attributes,callouts,macros"]
  27. ----
  28. include-tagged::{sql-specs}/docs/docs.csv-spec[case]
  29. ----
  30. ["source","sql",subs="attributes,callouts,macros"]
  31. ----
  32. include-tagged::{sql-specs}/docs/docs.csv-spec[caseReturnNull]
  33. ----
  34. ["source","sql",subs="attributes,callouts,macros"]
  35. ----
  36. include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithElse]
  37. ----
  38. As a variant, a case expression can be expressed with a syntax similar to *switch-case* of other programming languages:
  39. [source, sql]
  40. ----
  41. CASE expression
  42. WHEN value1 THEN result1
  43. [WHEN value2 THEN result2]
  44. [WHEN ...]
  45. [ELSE default_result]
  46. END
  47. ----
  48. In this case it's transformed internally to:
  49. [source, sql]
  50. ----
  51. CASE WHEN expression = value1 THEN result1
  52. [WHEN expression = value2 THEN result2]
  53. [WHEN ...]
  54. [ELSE default_result]
  55. END
  56. ----
  57. ["source","sql",subs="attributes,callouts,macros"]
  58. ----
  59. include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithOperand]
  60. ----
  61. ["source","sql",subs="attributes,callouts,macros"]
  62. ----
  63. include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithOperandAndElse]
  64. ----
  65. [NOTE]
  66. ===============================
  67. All result expressions must be of compatible data types. More specifically all result
  68. expressions should have a compatible data type with the 1st _non-null_ result expression.
  69. E.g.:
  70. for the following query:
  71. [source, sql]
  72. CASE WHEN a = 1 THEN null
  73. WHEN a > 2 THEN 10
  74. WHEN a > 5 THEN 'foo'
  75. END
  76. an error message would be returned, mentioning that *'foo'* is of data type *keyword*,
  77. which does not match the expected data type *integer* (based on result *10*).
  78. ===============================
  79. [[sql-functions-conditional-coalesce]]
  80. ==== `COALESCE`
  81. .Synopsis:
  82. [source, sql]
  83. ----
  84. COALESCE(
  85. expression, <1>
  86. expression, <2>
  87. ...)
  88. ----
  89. *Input*:
  90. <1> 1st expression
  91. <2> 2nd expression
  92. ...
  93. **N**th expression
  94. COALESCE can take an arbitrary number of arguments.
  95. *Output*: one of the expressions or `null`
  96. .Description
  97. Returns the first of its arguments that is not null.
  98. If all arguments are null, then it returns `null`.
  99. ["source","sql",subs="attributes,callouts,macros"]
  100. ----
  101. include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNonNull]
  102. ----
  103. ["source","sql",subs="attributes,callouts,macros"]
  104. ----
  105. include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNull]
  106. ----
  107. [[sql-functions-conditional-greatest]]
  108. ==== `GREATEST`
  109. .Synopsis:
  110. [source, sql]
  111. ----
  112. GREATEST(
  113. expression, <1>
  114. expression, <2>
  115. ...)
  116. ----
  117. *Input*:
  118. <1> 1st expression
  119. <2> 2nd expression
  120. ...
  121. **N**th expression
  122. GREATEST can take an arbitrary number of arguments and
  123. all of them must be of the same data type.
  124. *Output*: one of the expressions or `null`
  125. .Description
  126. Returns the argument that has the largest value which is not null.
  127. If all arguments are null, then it returns `null`.
  128. ["source","sql",subs="attributes,callouts,macros"]
  129. ----
  130. include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNonNull]
  131. ----
  132. ["source","sql",subs="attributes,callouts,macros"]
  133. ----
  134. include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNull]
  135. ----
  136. [[sql-functions-conditional-ifnull]]
  137. ==== `IFNULL`
  138. .Synopsis:
  139. [source, sql]
  140. ----
  141. IFNULL(
  142. expression, <1>
  143. expression) <2>
  144. ----
  145. *Input*:
  146. <1> 1st expression
  147. <2> 2nd expression
  148. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  149. .Description
  150. Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  151. Returns the first of its arguments that is not null.
  152. If all arguments are null, then it returns `null`.
  153. ["source","sql",subs="attributes,callouts,macros"]
  154. ----
  155. include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnFirst]
  156. ----
  157. ["source","sql",subs="attributes,callouts,macros"]
  158. ----
  159. include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnSecond]
  160. ----
  161. [[sql-functions-conditional-iif]]
  162. ==== `IFF`
  163. .Synopsis:
  164. [source, sql]
  165. ----
  166. IIF(expression<1>, expression<2>, [expression<3>])
  167. ----
  168. *Input*:
  169. <1> boolean condition to check
  170. <2> return value if the boolean condition evaluates to `true`
  171. <3> return value if the boolean condition evaluates `false`; optional
  172. *Output*: 2nd expression if 1st expression (condition) evaluates to `true`. If it evaluates to `false`
  173. return 3rd expression. If 3rd expression is not provided return `null`.
  174. .Description
  175. Conditional function that implements the standard _IF <condition> THEN <result1> ELSE <result2>_
  176. logic of programming languages. If the 3rd expression is not provided and the condition evaluates to `false`,
  177. `null` is returned.
  178. ["source","sql",subs="attributes,callouts,macros"]
  179. ----
  180. include-tagged::{sql-specs}/docs/docs.csv-spec[iifWithDefaultValue]
  181. ----
  182. ["source","sql",subs="attributes,callouts,macros"]
  183. ----
  184. include-tagged::{sql-specs}/docs/docs.csv-spec[iifWithoutDefaultValue]
  185. ----
  186. [TIP]
  187. =================
  188. *IIF* functions can be combined to implement more complex logic simulating the <<sql-functions-conditional-case>>
  189. expression. E.g.:
  190. [source, sql]
  191. IIF(a = 1, 'one', IIF(a = 2, 'two', IIF(a = 3, 'three', 'many')))
  192. =================
  193. [[sql-functions-conditional-isnull]]
  194. ==== `ISNULL`
  195. .Synopsis:
  196. [source, sql]
  197. ----
  198. ISNULL(
  199. expression, <1>
  200. expression) <2>
  201. ----
  202. *Input*:
  203. <1> 1st expression
  204. <2> 2nd expression
  205. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  206. .Description
  207. Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  208. Returns the first of its arguments that is not null.
  209. If all arguments are null, then it returns `null`.
  210. ["source","sql",subs="attributes,callouts,macros"]
  211. ----
  212. include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnFirst]
  213. ----
  214. ["source","sql",subs="attributes,callouts,macros"]
  215. ----
  216. include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnSecond]
  217. ----
  218. [[sql-functions-conditional-least]]
  219. ==== `LEAST`
  220. .Synopsis:
  221. [source, sql]
  222. ----
  223. LEAST(
  224. expression, <1>
  225. expression, <2>
  226. ...)
  227. ----
  228. *Input*:
  229. <1> 1st expression
  230. <2> 2nd expression
  231. ...
  232. **N**th expression
  233. LEAST can take an arbitrary number of arguments and
  234. all of them must be of the same data type.
  235. *Output*: one of the expressions or `null`
  236. .Description
  237. Returns the argument that has the smallest value which is not null.
  238. If all arguments are null, then it returns `null`.
  239. ["source","sql",subs="attributes,callouts,macros"]
  240. ----
  241. include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNonNull]
  242. ----
  243. ["source","sql",subs="attributes,callouts,macros"]
  244. ----
  245. include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNull]
  246. ----
  247. [[sql-functions-conditional-nullif]]
  248. ==== `NULLIF`
  249. .Synopsis:
  250. [source, sql]
  251. ----
  252. NULLIF(
  253. expression, <1>
  254. expression) <2>
  255. ----
  256. *Input*:
  257. <1> 1st expression
  258. <2> 2nd expression
  259. *Output*: `null` if the 2 expressions are equal, otherwise the 1st expression.
  260. .Description
  261. Returns `null` when the two input expressions are equal and
  262. if not, it returns the 1st expression.
  263. ["source","sql",subs="attributes,callouts,macros"]
  264. ----
  265. include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnFirst]
  266. ----
  267. ["source","sql",subs="attributes,callouts,macros"]
  268. ----
  269. include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnNull]
  270. ----
  271. [[sql-functions-conditional-nvl]]
  272. ==== `NVL`
  273. .Synopsis:
  274. [source, sql]
  275. ----
  276. NVL(
  277. expression, <1>
  278. expression) <2>
  279. ----
  280. *Input*:
  281. <1> 1st expression
  282. <2> 2nd expression
  283. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  284. .Description
  285. Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  286. Returns the first of its arguments that is not null.
  287. If all arguments are null, then it returns `null`.
  288. ["source","sql",subs="attributes,callouts,macros"]
  289. ----
  290. include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnFirst]
  291. ----
  292. ["source","sql",subs="attributes,callouts,macros"]
  293. ----
  294. include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnSecond]
  295. ----