SQL Server 字符串与 Null 的连接 [英] SQL Server String Concatenation with Null

查看:45
本文介绍了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 的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆