conditional.asciidoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ----