下面有一个简单的代码。
Select columnA, columnB, columnC
From table.
这是我的结果。
ColumnA ColumnB ColumnC Apple G null Juice S T9
Now, I use the concatenation as follow
Select
ColumnA + '_'+ ColumnB + '_' + ISNULL(ColumnC, '') as Name
From table
我的输出如下
Name Apple_G_ Juice_S_T9
如何修改上面的串联,使其显示为 Apple_G 而不是 Apple_G_ 基本上,我的结果中有一个额外的_。
最佳答案
在应用 IsNull
函数之前,只需将下划线与可为空的列连接起来即可。
Select
ColumnA + '_'+ ColumnB + ISNULL('_' + ColumnC, '') as Name
From table
这需要 CONCAT_NULL_YIELDS_NULL
选项设置为 ON。 - 这是默认的,也是 Microsoft 强烈推荐的。
关于sql - SQL Server 中与 Null 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37165294/