带Null的SQL Server字符串串联 [英] SQL Server String Concatenation with Null

查看:74
本文介绍了带Null的SQL Server字符串串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个跨字段的计算列,其中某些字段可能为空。

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, '')

如果列的值/ expression确实为NULL,然后将使用指定的第二个值(此处为空字符串)。

If the value of the column/expression is indeed NULL, then the second value specified (here: empty string) will be used instead.

这篇关于带Null的SQL Server字符串串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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