将两列数据合并为一列,保留空值

时间:2017-11-03 11:24:22

标签: sql sql-server tsql stored-procedures sql-server-2014

我的桌子有四列,如下所示

表格 - 订阅包含此类数据

  part_id     subscription   policylicense    enterpriselic
   part1        sub1          null                null
   part2        sub1          pl1                 null
   part3        sub1          null                enterpr1

我想获得如下所示的数据

  part_id     subscription   license    
   part2        sub1          pl1                 
   part3        sub1          enterpr1                

如何将合并的许可证数据合并到一个列中,在同一个表中保留空值...我在这里使用sql server

请有人帮忙解决这个问题,非常感谢我... 非常感谢提前..

7 个答案:

答案 0 :(得分:2)

以下是使用COALESCE()

的另一种解决方案
SELECT
    part_id
    ,subscription
    ,COALESCE(policylicense, enterpriselic) AS license
FROM subscription
WHERE COALESCE(policylicense, enterpriselic) IS NOT NULL

答案 1 :(得分:1)

Select 
    part_id,
    subscription,
    CONCAT(policylicense,enterpriselic) as license
from subscription where enterpriselic IS NOT NULL OR  policylicense IS NOT NULL

答案 2 :(得分:1)

试试这个:

SELECT part_id, subscription,
ISNULL(policylicense, enterpriselic) AS license
FROM yourtable
WHERE policylicense IS NOT NULL OR enterpriselicense IS NOT NULL

答案 3 :(得分:1)

试试这个。 Ternary operators

SELECT
part_id,
subscription,
IIF(policylicense IS NULL,
    IIF(enterpriselic IS NULL,'',enterpriselic),
        policylicense) as license
from subscription 
where   enterpriselic IS NOT NULL OR  
        policylicense IS NOT NULL

答案 4 :(得分:1)

试试这个COALESC()

SELECT part_id
    ,subscription
    ,COALESCE(policylicense, enterpriselic) AS license
FROM subscription
WHERE COALESCE(policylicense, enterpriselic) IS NOT NULL

答案 5 :(得分:0)

                    concurrencySemaphore.Wait(cancelToken);
                    deferRelease = false;
                    try
                    {
                        var result = GetWorkItem();
                        if (result == null)
                        { // no work, wait for new work or exit signal
                            signal = WaitHandle.WaitAny(signals);
                            continue;
                        }

                        deferRelease = true;
                        tasks.Add(Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                DoWorkHereAsync(result); // guess I'd think to .GetAwaiter().GetResult() here.. not run this yet
                            }
                            finally
                            {
                                concurrencySemaphore.Release();
                            }
                        }, cancelToken));
                    }
                    finally
                    {
                        if (!deferRelease)
                        {
                            concurrencySemaphore.Release();
                        }
                    }

答案 6 :(得分:0)

这是一种使用IF的方法,所以你要测试列是否为null,并且只是看到最后的结果为null

SELECT part_id, subscription, IF(policylicense IS NOT NULL, policylicense, IF(enterpriselic IS NOT NULL, enterpriselic, enterpriselic)) as license
FROM tableA
HAVING license IS NOT NULL;