Heim  >  Artikel  >  Datenbank  >  Verwendung von isnull in SQL

Verwendung von isnull in SQL

下次还敢
下次还敢Original
2024-05-02 00:27:32945Durchsuche

SQL 中 ISNULL 函数检查值是否为 NULL,并返回指定值(如果值为 NULL)或原始值(如果值为非 NULL)。它的语法为:ISNULL(expression, replacement_value)。该函数用于替换 NULL 值以提高可读性、防止错误和在聚合函数中处理 NULL 值。

Verwendung von isnull in SQL

SQL 中 ISNULL 函数的用法

什么是 ISNULL 函数?

ISNULL 函数是一种 SQL 函数,用于检查值是否为 NULL,并返回一个指定值(如果值为 NULL)或原始值(如果值为非 NULL)。

语法:

ISNULL(expression, replacement_value)

其中:

  • expression:要检查的值。
  • replacement_value:如果 expression 为 NULL,则返回的值。

用法:

ISNULL 函数可用于各种情况,例如:

  • 替换 NULL 值为更友好的值,以提高数据可读性。
  • 防止空值导致错误或意外结果。
  • 在聚合函数(如 SUM、COUNT)中处理 NULL 值。

示例:

考虑以下表格:

ID Name
1 John Smith
2 NULL

要替换 Name 列中为 NULL 的值,可以使用以下查询:

<code class="sql">SELECT ID, ISNULL(Name, 'Unknown') FROM TableName;</code>

输出:

ID Name
1 John Smith
2 Unknown

注意事项:

  • ISNULL 函数的第一个参数必须是表达式,不能是子查询。
  • 如果 replacement_value 为 NULL,ISNULL 函数将返回 expression 本身,而不会进行替换。
  • ISNULL 函数与 COALESCE 函数类似,但 COALESCE 函数可以接受多个 replacement_value。

Das obige ist der detaillierte Inhalt vonVerwendung von isnull in SQL. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn