AS NewCompoundField - 连接可能为 NULL 的值

AS NewCompoundField - Concatenating values which may be NULL

我有以下 T-SQL 查询;

SELECT      
    Site.Address1 + CHAR(13) + Site.Address2 + CHAR(13) + 
        Site.Address3 + CHAR(13)  + Site.Town + CHAR(13) + 
        Site.eCounty AS SiteAddress
FROM Site

如果构成 SiteAddress 的任何 Site.AddressX 字段是 NULL,那么 SiteAddress 本身就是 NULL。我想要一个 Site.AddressX 字段,如果它是 NULL.

,它后面的换行符将被忽略

我已经尝试为单个字段添加一个 CASE 语句并且它是换行符,但无法让它工作。

SELECT      
    (CASE WHEN (Site.Address1 IS NULL) THEN '' ELSE Site.Address1 + CHAR(10)) +
        Site.Address2 + CHAR(13) + 
        Site.Address3 + CHAR(13)  + Site.Town + CHAR(13) + 
        Site.eCounty AS SiteAddress
FROM Site

我该怎么做?

试试这个:

SELECT      
        COALESCE([Site].Address1 + CHAR(13), '')  + 
        COALESCE([Site].Address2 + CHAR(13), '')  + 
        COALESCE([Site].Address3 + CHAR(13), '')  + 
        COALESCE([Site].Town + CHAR(13), '') + 
        [Site].eCounty AS SiteAddress
FROM [Site]

试试这个

SELECT      
    CASE WHEN Site.Address1 IS NULL THEN '' 
    ELSE Site.Address1 + CHAR(10) +
        Site.Address2 + CHAR(13) + 
        Site.Address3 + CHAR(13)  + Site.Town + CHAR(13) + 
        Site.eCounty END AS SiteAddress 
FROM SITE Site