简体   繁体   English

MS SQL:从3列中检索“ Varchar”数据并将其组合在一起

[英]MS SQL: Retrieving “Varchar” data from 3 columns and combine them together

I have three columns in my sql table, "FirstName", "MiddleName","LastName". 我的SQL表中有三列,“名字”,“中间名”,“姓”。 When retrieving, I need to display these 3 together, for an example 检索时,我需要将这3个显示在一起,例如

FirstName = "John" MiddleName = "Ned" LastName = "Carter". FirstName =“ John” MiddleName =“ Ned” LastName =“ Carter”。

On retrieving, these should be displayed as "John Ned Carter". 检索时,这些应显示为“ John Ned Carter”。

I tried the following 我尝试了以下

select FirstName+MiddleName+LastName from PhoneData

There is a problem!!! 有一个问题!!! There are number of Names which the middle name is NULL. 有许多名称,中间名称为NULL。 There are number of names which the last name is NULL, and so on. 有许多名称,姓氏为NULL,依此类推。 This is not retrieving those!!! 这不是在找那些!!! It simply retrieve names where all the fields are not null!!!! 它只是检索所有字段都不为空的名称!!!! If at least one column is null for a particular name, then it shows the whole name as NULL!!! 如果特定名称的至少一列为空,则整个名称显示为NULL !!! For an example, 举个例子

FirstName = "John" MiddleName = NULL LastName = NULL FirstName =“ John” MiddleName = NULL LastName = NULL

on retrieval, the out put is ' NULL ' , not "John" 在检索时,输出为'NULL',而不是“ John”

Please help! 请帮忙!

Try the ISNULL() function around each field. 尝试在每个字段周围使用ISNULL()函数。 Then you can set a value for when the selected value is null. 然后,可以为选定的值为空时设置一个值。

Like this; 像这样;

select ISNULL(FirstName, '') + ISNULL(MiddleName, '') + ISNULL(LastName, '') from PhoneData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM