本文共 3059 字,大约阅读时间需要 10 分钟。
coalesce函数
Hey, folks! In this article, we will be focusing on SQL Server COALESCE() function.
嘿伙计! 在本文中,我们将重点介绍SQL Server COALESCE()函数 。
SQL Server COALESCE() function
helps to estimate and return the first NON-NULL value from the set of data or database.
SQL Server COALESCE() function
有助于估计并从数据或数据库集中返回第一个NON-NULL值。
In scenarios when we need to fetch or estimate the non-null values from the database, SQL Server COALESCE() function can be used to server the purpose.
在需要从数据库中获取或估计非空值的情况下,可以使用SQL Server COALESCE()函数来达到目的。
Having understood the working of COALESCE() function, let us get started with the syntax and implementation of the same.
了解了COALESCE()函数的工作原理之后,让我们开始介绍该语法和实现。
Select COALESCE(expressions)
The expressions includes the set of data values to test for the presence of first non-null values.
表达式包括一组数据值,以测试是否存在第一个非空值。
The COALESCE() function returns the first NON-NULL expression from the parameter list.
COALESCE()函数从参数列表返回第一个NON-NULL表达式 。
If all the expressions in the parameter list are NULL values, then the COALESCE() function returns NULL.
如果参数列表中的所有表达式均为NULL值,则COALESCE()函数将返回NULL。
Now let us understand the implementation of COALESCE() function through various examples as mentioned below.
现在,让我们通过下面提到的各种示例来了解COALESCE()函数的实现。
Example 1:
范例1:
SELECT COALESCE(NULL, 'Python', NULL, 'JournalDev.com');
In this example, we passed set of data values that includes two NULL values and two String values. The COALESCE() function returns the first NON-NULL value i.e. ‘Python’.
在此示例中,我们传递了一组包含两个NULL值和两个String值的数据值。 COALESCE()函数返回第一个NON-NULL值,即“ Python”。
Output:
输出:
Python
Example 2:
范例2:
SELECT COALESCE(NULL, NULL, NULL, 100);
In this example, we have passed three NULL values and an integer value. The function returns the integer value as output.
在此示例中,我们传递了三个NULL值和一个整数值。 该函数返回整数值作为输出。
Output:
输出:
100
Many a times, we tend to get confused between two important yet relating functions of SQL Server i.e. COALESCE() function and ISNULL() function.
很多时候,我们倾向于混淆两个重要但又相关SQL Server函数,即COALESCE()函数和ISNULL()函数。
SQL Server COALESCE() function
tests for the presence of first NON-NULL value and returns the value.
SQL Server COALESCE() function
测试是否有第一个NON-NULL值并返回该值。
On the other side, SQL Server ISNULL() function
checks whether the entered expression is NULL or not. If the entered expression is NULL, it returns the value passed to the argument list as output. Else, it returns the expression itself.
另一方面, SQL Server ISNULL() function
检查输入的表达式是否为NULL。 如果输入的表达式为NULL,它将返回传递到参数列表的值作为输出。 否则,它返回表达式本身。
By this, we have come to the end of this topic. Please feel free to comment below in case you come across any doubts.
至此,我们到了本主题的结尾。 如果您有任何疑问,请随时在下面发表评论。
For more such posts related to SQL Server, please do visit.
有关与SQL Server相关的更多此类帖子,请访问 。
翻译自:
coalesce函数
转载地址:http://poqzd.baihongyu.com/