如何在SQL Server中串联 [英] How to concatenate in SQL Server
问题描述
我的数据库没有特定的列,因此我通过switch在查询中创建了一个列。我需要将该列与数据库中的另一列连接起来:
My database doesn't have a specific column so I created a column in my query by switch. What I need is to concatenate this column with another column in the database:
select
certificateDuration ,
'DurationType' = case
when certificateDurationType = 0 then 'Day'
when certificateDurationType = 1 then 'Month'
when certificateDurationType = 2 then 'Year'
end
from
Scientific_Certification
推荐答案
在SQL中连接字符串服务器上,您只需使用 +运算符。
注意,如果子字符串之一为null,则整个串联的字符串也将变为null。因此,即使一个子字符串为空,也需要结果,请使用 COALESCE 。
To concatenate strings in SQL Server you can simply use the + operator.
Note that if one of the substrings is null then the entire concatenated string will become null as well. therefor, use COALESCE if you need a result even if one substring is null.
select certificateDuration,
' DurationType = '+
COALESCE(case
when certificateDurationType = 0 then 'Day'
when certificateDurationType = 1 then 'Month'
when certificateDurationType = 2 then 'Year'
end, '') As DurationType
from Scientific_Certification
注意:我对您的case子句使用了合并,因为您没有默认行为(由 else
指定)。这意味着如果 certificateDurationType
不为0、1或2,则case语句将返回 null
。
Note: I've used coalesce on your case clause since you have no default behavior (specified by else
). this means that if certificateDurationType
is not 0, 1 or 2 the case statement will return null
.
这篇关于如何在SQL Server中串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!