coalesce函数_什么是SQL Server COALESCE()函数?

coalesce函数

Hey, folks! In this article, we will be focusing on SQL Server COALESCE() function.

嘿伙计! 在本文中,我们将重点介绍SQL Server COALESCE()函数



了解SQL Server COALESCE()函数 (Understanding SQL Server COALESCE() function)

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()函数来达到目的。



SQL Server COALESCE()函数的语法 (Syntax of SQL Server COALESCE() function)

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。



COALESCE()函数的示例 (Examples of COALESCE() function)

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


COALESCE()v / s ISNULL() (COALESCE() v/s ISNULL() )

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,它将返回传递到参数列表的值作为输出。 否则,它返回表达式本身。



结论 (Conclusion)

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

有关与SQL Server相关的更多此类帖子,请访问SQL Server JournalDev



参考资料 (References)

翻译自: https://www.journaldev.com/41069/sql-server-coalesce

coalesce函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值