如何在sql

时间:2017-07-13 16:30:04

标签: sql sql-server sql-server-2008 tsql

这个问题在很多时候被问到,但我没有得到正确的输出,因为我期待:

我有一个表,我需要连接列,但我不想要NULL值。我想在sql server 2008中使用concate函数在2008年不起作用。

实施例: OrderTable

 Customer_Number   order1   order2  order3   order4 
    1               NULL      X       Y       NULL   
    2               NULL      A       B       NULL
    3                V       NULL     H       NULL 

现在我希望我想要的是连续方式的数据,只有这样的顺序:

Customer_Number     Order
  1                 X,Y
  2                 A,B
  3                 V,H

这是我使用的代码

 Select Customer_number, ISNULL(NULLIF(order1,' ')+',','')+
 ISNULL(NULLIF(order2,' ')+',','')+ 
 ISNULL(NULLIF(order3,' ')+',','')+
 ISNULL(NULLIF(order4,' ')+',','')
 as Order from Ordertable

我得到了以下输出

Customer_Number      Order
  1                NULL,X,Y,NULL
  2                NULL,A,B,NULL
  3                V,NULL,H,NULL

我已经尝试过Coalesce,Stuff,ISNULL,NULLIF,但都有相同的结果

提前致谢!!!

4 个答案:

答案 0 :(得分:3)

另一个变体,为了娱乐和利润,演示了在SQL Server 2012之前连接值的FOR XML技巧。

SELECT Customer_Number, STUFF(
    (SELECT ',' + order1, ',' + order2, ',' + order3, ',' + order4 FOR XML PATH('')), 
    1, 1, ''
)

这对于恒定数量的列来说有点过分(并且不是特别有效),但是用于连接的易于记忆的模式。此外,它显示了STUFF,这是任何SQL开发人员应该学会爱的函数。

答案 1 :(得分:1)

示例

 Declare @YourTable Table ([Customer_Number] varchar(50),[order1] varchar(50),[order2] varchar(50),[order3] varchar(50),[order4] varchar(50))
Insert Into @YourTable Values 
 (1,NULL,'X','Y',NULL)
,(2,NULL,'A','B',NULL)
,(3,'V',NULL,'H',NULL)


 Select Customer_Number   
       ,[Order] = IsNull(stuff(
                  IsNull(','+order1,'')
                 +IsNull(','+order2,'')
                 +IsNull(','+order3,'')
                 +IsNull(','+order4,'')
                 ,1,1,''),'')
 From  @YourTable

<强>返回

Customer_Number Order
1               X,Y
2               A,B
3               V,H
  

编辑 - 如果“NULL”是字符串而非NULL值

Declare @YourTable Table ([Customer_Number] varchar(50),[order1] varchar(50),[order2] varchar(50),[order3] varchar(50),[order4] varchar(50))
Insert Into @YourTable Values 
 (1,'NULL','X','Y','NULL')
,(2,'NULL','A','B','NULL')
,(3,'V','NULL','H','NULL')


 Select Customer_Number   
       ,[Order] = IsNull(stuff(
                  IsNull(','+nullif(order1,'NULL'),'')
                 +IsNull(','+nullif(order2,'NULL'),'')
                 +IsNull(','+nullif(order3,'NULL'),'')
                 +IsNull(','+nullif(order4,'NULL'),'')
                 ,1,1,''),'')
 From  @YourTable

答案 2 :(得分:0)

这有点令人不快,它需要在子查询中有效地删除尾随逗号(尽管你可以使用CTE):

SELECT 
    Customer_number, 
    SUBSTRING([Order], 0, LEN([Order])) AS [Order]
FROM(
    SELECT 
        Customer_number,
        COALESCE(order1+',', '') +
        COALESCE(order2+',', '') +
        COALESCE(order3+',', '') +
        COALESCE(order4+',', '') AS [Order]
    FROM
        OrderTable) AS SubQuery

答案 3 :(得分:0)

你使用Coalesce或IsNull走在正确的轨道上。我没有尝试跟踪长度并使用子字符串,左侧或东西,而是使用了一个替换来删除将从合并中显示的尾随逗号。

Select Customer_number, 
    Replace(
        Coalesce(Order1 + ',','')+
        Coalesce(Order2 + ',','')+
        Coalesce(Order3 + ',','')+
        Coalesce(Order4 + ',','')
     +',',',,','') --Hack to remove the Last Comma
 as [Order] from Ordertable