如何在SQL Server中连接

时间:2015-05-03 08:46:40

标签: sql sql-server tsql concatenation

我的数据库没有特定的列,所以我通过switch在查询中创建了一个列。我需要的是将此列与数据库中的另一列连接:

select 
    certificateDuration ,
   'DurationType' = case
                      when certificateDurationType = 0 then 'Day' 
                      when certificateDurationType = 1 then 'Month'
                      when certificateDurationType = 2 then 'Year'
                    end
from 
    Scientific_Certification

2 个答案:

答案 0 :(得分:1)

您必须尝试使用​​此SQL查询来联系列。在这里,我合并了名为user_name的用户表的三列(name_first,name_middle,name_last),并将结果放入名为FULLNAME的单个列中。

Code for SQL query

这是结果的图片

Result

答案 1 :(得分:0)

要在SQL Server中连接字符串,您只需使用+ operator 请注意,如果其中一个子字符串为null,则整个连接字符串也将变为null。因此,如果您需要结果,即使一个子字符串为空,也请使用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

注意:我在您的case子句中使用了coalesce,因为您没有默认行为(由else指定)。这意味着如果certificateDurationType不是0,1或2,则case语句将返回null