Sql Query从表Employee消除Null值中获取全名

时间:2015-07-16 12:07:19

标签: sql sql-server

我有一张EmpName表。

Select * from EmpName

NameID FirstName MiddleName LastName
1       Sam         NULL      NULL
2       NULL        Todd      Tarzan
3       NULL        NULL      Sare
4       Ben         Parker    NULL
5       James       Nick      Nancy

现在我编写以下查询以获取全名

Select FirstName + ' ' + MiddleName + ' ' + LastName 
As FullName from EmpName

但我得到以下结果 -

FullName
   NULL
   NULL
   NULL
   NULL
James Nick Nancy

但我想要以下结果 -

FullName
   Sam
Todd Tarzan
   Sare
Ben Parker
James Nick Nancy

StringNull联合返回Null吗?

如何获得FullNameMiddleName的值为空的LastName

9 个答案:

答案 0 :(得分:3)

连接字符串时,public static DatabaseHandler getInstance(Context ctx) { // Use the application context, which will ensure that you // don't accidentally leak an Activity's context. if (mInstance == null) { mInstance = new DatabaseHandler(ctx.getApplicationContext()); } return mInstance; } 优先。所以,使用NULL。这对分隔符来说是一种痛苦,但以下应该做你想做的事情:

COALESCE()

答案 1 :(得分:0)

您需要使用MySQL String函数,例如CONCAT()。

示例:

mysql> SELECT CONCAT('My', 'S', 'QL');

此处提供更多信息:https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

答案 2 :(得分:0)

关于null的问题尝试此查询

Select ltrim(coalesce(' ' + FirstName, '') +
             coalesce(' ' + MiddleName, '') +
             coalesce(' ' + LastName)
            ) as FullName
From EmpName;

答案 3 :(得分:0)

您可以使用length来避免以下COALESCE

NULLS

答案 4 :(得分:0)

NULL通过串联表达式传播,因此您的结果将通过NULL

传递

为了达到你需要使用ISNULL或COALESCE后的目的: -

select sc.[Actual Posting Date], sc.[Product Code]
from SalesCurrent sc
where sc.[Customer Account] = 'BLUF008'
and sc.[Accounting Branch] = 117
AND exists
(
 SELECT 1 
 FROM SIC_ProductDetails p
 WHERE p.SIC_IdSupportedInitiative = 61
       AND p.SIC_SupplierProductCode = sc.[Product Code]
       OR p.SIC_SupplierProductCode = 
       (
        SELECT sp.SIC_SupplierProductCode 
        FROM SIC_SupplierProducts sp 
        WHERE sp.SIC_IdSupplier = 6
        AND sp.SIC_SupplierProductCode = sc.[Product Code]
       )
)

OR

Select ISNULL(FirstName,'') + ' ' + ISNULL(MiddleName,'') + ' ' + ISNULL(LastName,'')
As FullName from EmpName

答案 5 :(得分:0)

public class EntityClassMap : ClassMap<Entity> { public EntityClassMap() { Id(x => x.Id); Map(x => x.Ref); Map(x => x.Field1); } } 添加到字符串(或其他任何内容)时,结果为null

null

您可以使用isnull functionselect 'a' + null --> null 列提供替代值。通过在null调用中添加+ ' ',只有在该部分名称不为空时才会获得空格:

isnull

答案 6 :(得分:0)

这取决于您使用的数据库,

- SQL Server / Microsoft Access -

SELECT FirstName + ' ' + LastName As FullName FROM Employee

- Oracle -

SELECT FirstName || ' ' || LastName As FullName FROM Employee

- MySQL ---

SELECT CONCAT(FirstName, ' ', LastName) As FullName FROM Employee

答案 7 :(得分:0)

也可以尝试:

DECLARE @FirstName VARCHAR(10) = '  John  '
DECLARE @MiddleName VARCHAR(10) = NULL-- '  David   '
DECLARE @LastName VARCHAR(10) = '    A    '
DECLARE @FullName VARCHAR(100)

SELECT @FullName = COALESCE(LTRIM(RTRIM(@FirstName)), '') + 
                   COALESCE(' ' + LTRIM(RTRIM(@MiddleName)), '') + 
                   COALESCE(' ' + LTRIM(RTRIM(@LastName)), '')

SELECT @FullName AS [Name], LEN(@FullName) AS [Length]

查询

SELECT
    FullName = COALESCE(LTRIM(RTRIM(FirstName)), '') + 
               COALESCE(' ' + LTRIM(RTRIM(MiddleName)), '') + 
               COALESCE(' ' + LTRIM(RTRIM(LastName)), '')
FROM
    Employee

答案 8 :(得分:0)

select ISNULL(FIRSTNAME,'') + 
        (CASE WHEN ISNULL(FIRSTNAME,'') = '' THEN ISNULL(LEFT(MIDDLENAME,1),'') ELSE ' ' + ISNULL(LEFT(MIDDLENAME,1),'') END) + 
        (CASE WHEN ISNULL(FIRSTNAME+MIDDLENAME,'') = '' THEN LASTNAME WHEN Isnull(MiddleName,'') = '' THEN LASTNAME 
         ELSE ' ' + LASTNAME END) AS FullName 
FROM tblEmp