如果一个或多个列不为null,则sql连接列

时间:2018-03-20 17:08:52

标签: sql sql-server-2012

我有一张看起来像这样的表:

@GrabResolver(name='mymirror', root='http://myartifactory/public/')
@Grab(group='groupid', module='artifactid', version='1.2.3')

String grapeCacheDir = "${System.getProperty('user.home')}/.groovy/grapes"
String group = 'groupid'
String module = 'artifactid'
String version = '1.2.3'

File myJar = new File("$grapeCacheDir/$group/$module/jars/${module}-${version}.jar")
println myJar.path

我希望Notes列只有在列不为空时才是第二列和第四列的串联。

BuildingID | RouterType | RouterPort | RouterInstaller | Notes
-----------+------------+------------+-----------------+-------
282        | Linksys    | 1990       | Super           | NULL
307        | Sonic Wall | NULL       | Greg            | NULL
311        | NULL       | NULL       | NULL            | NULL

如果路由器类型的值不为空等,“路由器类型:”这个词也应该出现。

我对SQL很陌生 - 任何帮助都会非常感激。

5 个答案:

答案 0 :(得分:2)

试试这个:

select case when [Note] = '' then null else Note from (
    select BulidingId,
            case when RouterType is null then '' else 'Router Type: ' + RouterType + '; '+
            case when RouterPort is null then '' else 'Router Port: ' + RouterPort + '; '+
            case when RouterInstaller is null then '' else 'Router Port: ' + RouterInstaller + '; '+
            case when Notes is null then '' else 'Notes: ' + Notes + '; ' [Note]
    from MY_TABLE
) a

答案 1 :(得分:1)

这将通过结合Coalesce和Concat来实现。列名称作为标签添加到列值中。

select COALESCE(Notes, COALESCE(CONCAT(COALESCE(CONCAT('RouterType: ',RouterType),''), 
                       COALESCE(CONCAT(' RouterPort: ',RouterPort ),''),
                       COALESCE(CONCAT(' RouterInstaller: ',RouterInstaller),'')), NULL)) as Notes
from yourTable;

答案 2 :(得分:0)

尝试此更新声明:

DECLARE @TEMP_TABLE TABLE (
    BUILDING_ID INT,
    ROUTER_TYPE VARCHAR(20) NULL,
    PORT INT NULL,
    ROUTER_INSTALLER VARCHAR(20) NULL,
    NOTES VARCHAR(1000) NULL
)

INSERT INTO @TEMP_TABLE VALUES(1,'Linksys Router', 1990, 'Super', NULL)
INSERT INTO @TEMP_TABLE VALUES(2,NULL, NULL, NULL, NULL)

UPDATE @TEMP_TABLE 
SET NOTES = COALESCE(' Router type: ' + ROUTER_TYPE,'') + COALESCE(' Port: ' + CAST(PORT AS VARCHAR),'') + COALESCE(' Router installer: ' + ROUTER_INSTALLER,'')
WHERE ROUTER_TYPE IS NOT NULL OR PORT IS NOT NULL OR ROUTER_INSTALLER IS NOT NULL

SELECT * FROM @TEMP_TABLE

答案 3 :(得分:0)

尝试此选择查询,我认为它会对您有所帮助:

SELECT CASE WHEN (
  COL2 IS NOT NULL 
   AND COL3 IS NOT NULL 
   AND  COL4 IS NOT NULL )
    THEN 
        CONCAT(COL2,' ', COL3,' ', COL4) END as ConcatedData,
     * from YOUR_TABLE;

答案 4 :(得分:0)

为了使间距正确,我建议:

select stuff(coalesce(' RouterType: ' + RouterType), '') +
             coalesce(' RouterPort: ' + RouterPort ), '') +
             coalesce(' RouterInstaller: ', RouterInstaller), ''),
             1, 1, ''
            ) as Notes
from t;

update

update t
    set notes = stuff(coalesce(' RouterType: ' + RouterType), '') +
                      coalesce(' RouterPort: ' + RouterPort ), '') +
                      coalesce(' RouterInstaller: ', RouterInstaller), ''),
                      1, 1, ''
                     );

注意:这不会放入NULL值,而是使用空字符串。这很容易解决 - 如果这是一个问题:

update t
    set notes = nullif(stuff(coalesce(' RouterType: ' + RouterType), '') +
                             coalesce(' RouterPort: ' + RouterPort ), '') +
                             coalesce(' RouterInstaller: ', RouterInstaller), ''),
                             1, 1, ''
                            ), ''
                      )