如何将多个列值连接成一个列?

时间:2015-11-12 15:03:20

标签: asp.net sql-server visual-studio-2012 concatenation

我有下表朋友

id | first_name | last_name | gender | age | mobile
1  | bobby      | roe       | male   | 21  | 541-5780

如何将多列(first_name& last_name)值连接到一个列中以获得以下结果?

full_name
bobby roe 

我已经写了以下查询,但它不起作用

declare @full_name varchar(max)
select @full_name = COALESCE(@full_name + ', ', '') + first_name, last_name
from friend
select @full_name

1 个答案:

答案 0 :(得分:5)

实现这一目标的方法不止一种:

SELECT CONCAT(first_name, ' ' ,last_name) AS full_name;

对于早期版本(其中CONCAT不是内置函数):

SELECT first_name + ISNULL(' ' + last_name, '') as Full_Name from [YourTable]

这也应该给你相同的结果

SELECT COALESCE(first_name, '') + COALESCE(last_name, '') as FullName FROM [YourTable]