conditional.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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(
  167. expression, <1>
  168. expression, <2>
  169. [expression]) <3>
  170. ----
  171. *Input*:
  172. <1> boolean condition to check
  173. <2> return value if the boolean condition evaluates to `true`
  174. <3> return value if the boolean condition evaluates `false`; optional
  175. *Output*: 2nd expression if 1st expression (condition) evaluates to `true`. If it evaluates to `false`
  176. return 3rd expression. If 3rd expression is not provided return `null`.
  177. .Description
  178. Conditional function that implements the standard _IF <condition> THEN <result1> ELSE <result2>_
  179. logic of programming languages. If the 3rd expression is not provided and the condition evaluates to `false`,
  180. `null` is returned.
  181. ["source","sql",subs="attributes,callouts,macros"]
  182. ----
  183. include-tagged::{sql-specs}/docs/docs.csv-spec[iifWithDefaultValue]
  184. ----
  185. ["source","sql",subs="attributes,callouts,macros"]
  186. ----
  187. include-tagged::{sql-specs}/docs/docs.csv-spec[iifWithoutDefaultValue]
  188. ----
  189. [TIP]
  190. =================
  191. *IIF* functions can be combined to implement more complex logic simulating the <<sql-functions-conditional-case>>
  192. expression. E.g.:
  193. [source, sql]
  194. IIF(a = 1, 'one', IIF(a = 2, 'two', IIF(a = 3, 'three', 'many')))
  195. =================
  196. [[sql-functions-conditional-isnull]]
  197. ==== `ISNULL`
  198. .Synopsis:
  199. [source, sql]
  200. ----
  201. ISNULL(
  202. expression, <1>
  203. expression) <2>
  204. ----
  205. *Input*:
  206. <1> 1st expression
  207. <2> 2nd expression
  208. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  209. .Description
  210. Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  211. Returns the first of its arguments that is not null.
  212. If all arguments are null, then it returns `null`.
  213. ["source","sql",subs="attributes,callouts,macros"]
  214. ----
  215. include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnFirst]
  216. ----
  217. ["source","sql",subs="attributes,callouts,macros"]
  218. ----
  219. include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnSecond]
  220. ----
  221. [[sql-functions-conditional-least]]
  222. ==== `LEAST`
  223. .Synopsis:
  224. [source, sql]
  225. ----
  226. LEAST(
  227. expression, <1>
  228. expression, <2>
  229. ...)
  230. ----
  231. *Input*:
  232. <1> 1st expression
  233. <2> 2nd expression
  234. ...
  235. **N**th expression
  236. LEAST can take an arbitrary number of arguments and
  237. all of them must be of the same data type.
  238. *Output*: one of the expressions or `null`
  239. .Description
  240. Returns the argument that has the smallest value which is not null.
  241. If all arguments are null, then it returns `null`.
  242. ["source","sql",subs="attributes,callouts,macros"]
  243. ----
  244. include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNonNull]
  245. ----
  246. ["source","sql",subs="attributes,callouts,macros"]
  247. ----
  248. include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNull]
  249. ----
  250. [[sql-functions-conditional-nullif]]
  251. ==== `NULLIF`
  252. .Synopsis:
  253. [source, sql]
  254. ----
  255. NULLIF(
  256. expression, <1>
  257. expression) <2>
  258. ----
  259. *Input*:
  260. <1> 1st expression
  261. <2> 2nd expression
  262. *Output*: `null` if the 2 expressions are equal, otherwise the 1st expression.
  263. .Description
  264. Returns `null` when the two input expressions are equal and
  265. if not, it returns the 1st expression.
  266. ["source","sql",subs="attributes,callouts,macros"]
  267. ----
  268. include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnFirst]
  269. ----
  270. ["source","sql",subs="attributes,callouts,macros"]
  271. ----
  272. include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnNull]
  273. ----
  274. [[sql-functions-conditional-nvl]]
  275. ==== `NVL`
  276. .Synopsis:
  277. [source, sql]
  278. ----
  279. NVL(
  280. expression, <1>
  281. expression) <2>
  282. ----
  283. *Input*:
  284. <1> 1st expression
  285. <2> 2nd expression
  286. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  287. .Description
  288. Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  289. Returns the first of its arguments that is not null.
  290. If all arguments are null, then it returns `null`.
  291. ["source","sql",subs="attributes,callouts,macros"]
  292. ----
  293. include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnFirst]
  294. ----
  295. ["source","sql",subs="attributes,callouts,macros"]
  296. ----
  297. include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnSecond]
  298. ----