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)

  • SQL Server COALESCE() function — DocumentationSQL Server COALESCE()函数—文档

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

coalesce函数

coalesce函数_什么是SQL Server COALESCE()函数?相关推荐

  1. oracle sql常用的函数,界别Oracle和SQL Server常用函数

    区分Oracle和SQL Server常用函数 一.数学函数 1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) ...

  2. sql数据库查询聚合函数_如何使用SQL Server数据质量服务确保正确的数据聚合

    sql数据库查询聚合函数 介绍 (Introduction) An interesting opportunity arose at a client site during early Octobe ...

  3. sql dateadd函数_什么是SQL Server DATEADD()函数?

    sql dateadd函数 Hey, folks! In this article, we will be focusing on SQL Server DATEADD() function in d ...

  4. mysql coalesce函数用法,SQL Server COALESCE函数详解及实例

    SQL Server COALESCE函数详解 很多人知道ISNULL函数,但是很少人知道Coalesce函数,人们会无意中使用到Coalesce函数,并且发现它比ISNULL更加强大,其实到目前为止 ...

  5. sql server charindex函数和patindex函数详解(转)

    charindex和patindex函数常常用来在一段字符中搜索字符或字符串.假如被搜索的字符中包含有要搜索的字符,那么这两个函数返回一个非零的整数,这个整数是要搜索的字符在被搜索的字符中的开始位数. ...

  6. Sql Server REPLACE函数的使用;SQL中 patindex函数的用法

    Sql Server REPLACE函数的使用 REPLACE 用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法 REPLACE ( ''string_replace1' ...

  7. SQL Server用户自定义函数

    用户自定义函数不能用于执行一系列改变数据库状态的操作,但它可以像系统 函 数一样在查询或存储过程等的程序段中使用,也可以像存储过程一样通过 EXECUTE 命令来执行.在 SQL Server 中根据 ...

  8. SQL Server CONVERT() 函数,Date 函数

    From: http://www.w3school.com.cn/sql/func_convert.asp 定义和用法 CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() ...

  9. SQL Server日期函数集合

    SQL Server日期函数集合--1:获取系统日期和时间值函数 --getdate() SELECT GETDATE() AS 'today' --getutcdate() SELECT GETUT ...

最新文章

  1. iis6如何升级iis7_IIS修复IIS出现错误后如何完全卸载重装
  2. 【组队学习】【28期】R语言数据科学
  3. 数据中心网络架构 — 云网一体化数据中心网络 — 算力网络 — SDN 架构
  4. Hyper-V 配置网络 (高级篇)
  5. Python 堡垒机介绍
  6. SDNU 1263.C语言程序设计教程(第三版)课后习题10.5(约瑟夫环)
  7. MySQL 高级 - 索引 - 索引分类
  8. a标签隐藏真实地址_家庭影院布线非常杂乱应该怎么补救?A/V电缆管理的7个技巧...
  9. 微软工程师测试题——未来
  10. 卡巴斯基实验室2017年威胁预测
  11. 计算机专业高级职称评定条件,计算机高级工程师职称评定条件
  12. django实现qq一键登录(qq互联)
  13. python获取模块的名称_python获取当前模块的名称
  14. TM1650+DS3231+STC15LE计数数码管小时钟
  15. 《移动游戏开发精要》读书笔记
  16. java常用命令汇总
  17. SUB-1G无线射频收发器芯片DP4301/CMT2300A无线遥控器应用
  18. 博彦科技以太机器人平台亮相世界人工智能大会 赋能企业智慧升级
  19. Firefox 检测到潜在的安全威胁,并因 www.baidu.com 要求安全连接而没有继续。
  20. 华为Forsion Compute笔记

热门文章

  1. [转载] python机器学习库sklearn——参数优化(网格搜索GridSearchCV、随机搜索RandomizedSearchCV、hyperopt)
  2. js获取字符串出现最多的字符和次数
  3. 数据库查询·聚合分支格式化日期·思维导图要点误点(含示例)
  4. 启动tomcat时遇到的问题
  5. CSRF——攻击与防御
  6. sql2005安装过程,(不装C盘)
  7. http请求头中包含未编码中文时webapi self host崩溃
  8. Debian/Ubuntu Apache Mod_Rewrite 安装
  9. 【从理论到代码】旋转矩阵与欧拉角 一
  10. Git基础教程(三)