SQL Server String Concatenation with Null(SQL Server 字符串与 Null 的连接)
问题描述
我正在跨字段创建一个计算列,其中一些字段可能为空.
I am creating a computed column across fields of which some are potentially null.
问题在于,如果这些字段中的任何一个为空,则整个计算列将为空.我从 Microsoft 文档中了解到这是预期的并且可以通过设置 SET CONCAT_NULL_YIELDS_NULL 关闭.但是,我不想更改此默认行为,因为我不知道它对 SQL Server 其他部分的影响.
The problem is that if any of those fields is null, the entire computed column will be null. I understand from the Microsoft documentation that this is expected and can be turned off via the setting SET CONCAT_NULL_YIELDS_NULL. However, there I don't want to change this default behavior because I don't know its implications on other parts of SQL Server.
有没有办法让我只检查一列是否为空,并且只在计算列公式中附加它的内容,如果它不为空?
Is there a way for me to just check if a column is null and only append its contents within the computed column formula if its not null?
推荐答案
您可以使用 ISNULL(....)
SET @Concatenated = ISNULL(@Column1, '') + ISNULL(@Column2, '')
如果列/表达式的值确实为 NULL,则将使用指定的第二个值(此处:空字符串).
If the value of the column/expression is indeed NULL, then the second value specified (here: empty string) will be used instead.
这篇关于SQL Server 字符串与 Null 的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server 字符串与 Null 的连接


基础教程推荐
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01