[英]How to concatenate in SQL Server
我的数据库没有特定的列,所以我通过 switch 在我的查询中创建了一个列。 我需要的是将此列与数据库中的另一列连接起来:
select
certificateDuration ,
'DurationType' = case
when certificateDurationType = 0 then 'Day'
when certificateDurationType = 1 then 'Month'
when certificateDurationType = 2 then 'Year'
end
from
Scientific_Certification
要在 SQL Server 中连接字符串,您只需使用+ 运算符即可。
请注意,如果子字符串之一为空,则整个连接的字符串也将变为空。 因此,如果您需要结果,即使一个子字符串为空,也请使用COALESCE 。
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
注意:由于您没有默认行为(由else
指定),因此我在您的 case 子句上使用了 coalesce 。 这意味着如果certificateDurationType
不是 0、1 或 2,则 case 语句将返回null
。
您只需要尝试+
运算符或CONCAT
函数
+
适用于所有 SQL Server 版本
DurationType = ' + 'some text'
SQL Server 2012 + CONCAT
CONCAT('DurationType = ', 'some text')
您的查询应该是这样的
SELECT certificateDuration
,'DurationType = ' +
CASE certificateDurationType
WHEN 0 THEN 'Day'
WHEN 1 THEN 'Month'
WHEN 2 THEN 'Year'
END
FROM Scientific_Certification
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.