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.

相关文章