docs-v2/content/influxdb/cloud-dedicated/reference/sql/functions/conditional.md

2.3 KiB

title list_title description menu weight
SQL conditional functions Conditional functions Use conditional functions to conditionally handle null values in SQL queries.
influxdb_cloud_dedicated
name parent
Conditional sql-functions
306

The InfluxDB SQL implementation supports the following conditional functions for conditionally handling null values:

coalesce

Returns the first of its arguments that is not null. Returns null if all arguments are null. This function is often used to substitute a default value for null values.

coalesce(expression1[, ..., expression_n])
Arguments
  • expression1, expression_n: Expression to use if previous expressions are null. Can be a constant, column, or function, and any combination of arithmetic operators. Pass as many expression arguments as necessary.

{{< expand-wrapper >}} {{% expand "View coalesce query example" %}}

SELECT
  val1,
  val2,
  val3,
  coalesce(val1, val2, val3, 'quz') AS coalesce
FROM
  (values ('foo', 'bar', 'baz'),
          (NULL, 'bar', 'baz'),
          (NULL, NULL, 'baz'),
          (NULL, NULL, NULL)
  ) data(val1, val2, val3)
val1 val2 val3 coalesce
foo bar baz foo
bar baz bar
baz baz
quz

{{% /expand %}} {{< /expand-wrapper >}}

nullif

Returns null if expression1 equals expression2; otherwise it returns expression1. This can be used to perform the inverse operation of coalesce.

nullif(expression1, expression2)
Arguments
  • expression1: Expression to compare and return if equal to expression2. Can be a constant, column, or function, and any combination of arithmetic operators.
  • expression2: Expression to compare to expression1. Can be a constant, column, or function, and any combination of arithmetic operators.

{{< expand-wrapper >}} {{% expand "View nullif query example" %}}

SELECT
  value,
  nullif(value, 'baz') AS nullif
FROM
  (values ('foo'),
          ('bar'),
          ('baz')
  ) data(value)
value nullif
foo foo
bar bar
baz

{{% /expand %}} {{< /expand-wrapper >}}