在SQL Server 2005中,在SELECT语句中连接两个列。

5

如何在SQL Server 2005中的选择语句中合并两列?

以下是我的语句:Select FirstName,secondName from Table...

现在我尝试使用以下方法将secondNameFirstName合并:

Select FirstName + ' ' + secondName from Table

但是在一些记录的secondName列中,一些值是NULL。我的选择语句返回NULL而不是FirstName。如果secondName是NULL,我想要得到FirstName

3个回答

14

选择表中的 FirstName 字段并将其与 SecondName 字段相加,如果 SecondName 为空则不加空格。


0
如果您的字段之一是数字,则可以将其转换为字符串,方法如下:string
SELECT FirstName + ISNULL(' ' + SecondName, '') + ' age(' + CONVERT(nvarchar,age) + ')' from Table

0

像这样做:

 select cast( FirstName as varchar)+' '+cast( secondName as varchar) from table

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接