How to concatenate in SQL Server

前端 未结 3 671
庸人自扰
庸人自扰 2021-01-28 01:34

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:



        
3条回答
  •  不知归路
    2021-01-28 01:41

    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
    

    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.

提交回复
热议问题