【问题标题】:How to not return null value in sql server?如何在 sql server 中不返回空值?
【发布时间】:2017-07-04 06:49:03
【问题描述】:

我有如下所示的数据集;

公司 |地址 |商业 |电话号码 |联系人

A&B     | Perak   | Khmer Restaurants   | 012541           | Mr. Yu Lee
A&B     | Perak   | F&B                 | 012541           | Mr. Yu Lee
King Co.| Ipoh    | Paper Distributors  | 021453           | Mrs. Cheng
King Co.| Ipoh    | Paper Distributors  | Null             | Mrs. Cheng
DinoX   | Sunway  | Guesthouses         | 0124587          | Mr. Hong
Dinox   | Sunway  | Guesthouses         | 0124587          | Mr. Q

进行如下所示的查询后,我得到了一个新数据集:

IF OBJECT_ID('tempdb..#tCat') IS NOT NULL DROP TABLE #tcat
GO
DECLARE @portal varchar(100) = 'A4E7890F-A188-4663-89EB-176D94DF6774'
SELECT * INTO #tcat
FROM (
SELECT DISTINCT  list.[name]
                ,lc.listing_uuid
                ,dCat.[name]  as Category
                ,catg.[name]  as Sub_Category
                ,comm.[value] as Telephone_Number
                ,pp.title + ' ' + pp.first_name + ' ' + pp.last_name as Contact_Person
FROM panpages.listings as list
LEFT JOIN panpages.listing_categories as lc on lc.listing_uuid=list.uuid AND lc.portal_uuid=@portal
LEFT JOIN panpages.categories as catg on catg.uuid=lc.category_uuid AND catg.portal_uuid=@portal
left join panpages.listing_people as lp on lp.listing_uuid = list.uuid
left join panpages.people as pp on pp.id = lp.person_id
left join panpages.person_communications as comm on comm.person_id = lp.person_id and (comm.communication_type = 'Mobile Phone' or comm.communication_type = 'Tel')
LEFT JOIN ( SELECT DISTINCT uuid,[name] FROM panpages.categories WHERE parent_uuid IS NULL ) as dCat on dCat.uuid=catg.parent_uuid
WHERE list.portal_uuid=@portal and list.is_active=1
)as tCat

select
     list.[name] as [Company]
    ,list.[address] as [Address]
    ,replace(cats.Sub_Category,'&','&')     as [Nature of Business]
    ,replace(cats.Telephone_Number,'&','&') as [Telephone Number]
    ,replace(cats.Contact_Person,'&','&')   as [Contact Person]

from [panpages].[listings] as list
left join ( 

            SELECT DISTINCT tc1.listing_uuid,tc1.[name],
            Sub_Category = STUFF(( SELECT   ',   ' + tc2.Sub_Category
            FROM
                (
                SELECT Sub_Category, MIN(listing_uuid) AS listing_uuid
                FROM #tCat
                GROUP BY Sub_Category
                ) AS tc2 
                WHERE tc1.listing_uuid = tc2.listing_uuid  
                ORDER BY tc2.Sub_Category 
                FOR XML PATH('')), 1, 1, ''),
            Telephone_Number = STUFF(( SELECT   ',  ' + tc2.Telephone_Number
            FROM
                (
                SELECT Telephone_Number, MAX(listing_uuid) AS listing_uuid
                FROM #tCat
                GROUP BY Telephone_Number
                ) AS tc2 
                WHERE tc1.listing_uuid = tc2.listing_uuid  
                ORDER BY tc2.Telephone_Number 
                FOR XML PATH('')), 1, 1, ''),
            Contact_Person = STUFF(( SELECT   ',  ' + tc2.Contact_Person
            FROM
                (
                SELECT Contact_Person, MAX(listing_uuid) AS listing_uuid
                FROM #tCat
                GROUP BY Contact_Person
                ) AS tc2 
                WHERE tc1.listing_uuid = tc2.listing_uuid  
                ORDER BY tc2.Contact_Person 
                FOR XML PATH('')), 1, 1, '')        
            FROM #tCat as tc1 where tc1.listing_uuid is not null
        ) cats on cats.listing_uuid=list.uuid
where
    list.[portal_uuid]=@portal and
    list.[is_active]=1 

下面是新的数据集:

Company |Address| Business              | Telephone Number | Contact Person

A&B     | Perak | Khmer Restaurants, F&B| 012541           | Mr. Yu Lee
King Co.| Ipoh  | Paper Distributors    | 021453, Null     | Mrs. Cheng
DinoX   | Sunway| Guesthouses           | 0124587          | Mr. Hong, Mr Q

如何不返回空值?我不想返回“Null”值。

【问题讨论】:

  • 如果NULL,你还想返回什么?

标签: sql-server


【解决方案1】:

您可以在检查之前使用 ISNULL 函数,也可以使用 COALESCE 函数,如下所示

   SELECT COALESCE(Telephone_Number,''), MAX(listing_uuid) AS listing_uuid

在“ ”中替换您想要返回的任何内容。如果您只想要一个空字符串,请将其保留在 ''

【讨论】:

    【解决方案2】:

    在替换中使用 STUFF 来替换空值。

            Telephone_Number = Replace(STUFF( SELECT   ',  ' + tc2.Telephone_Number
            FROM
                (
                SELECT Telephone_Number, MAX(listing_uuid) AS listing_uuid
                FROM #tCat
                GROUP BY Telephone_Number
                ) AS tc2 
                WHERE tc1.listing_uuid = tc2.listing_uuid  
                ORDER BY tc2.Telephone_Number 
                FOR XML PATH('')), 1, 1, ''), ' null', ''),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-03
      • 2012-12-19
      • 1970-01-01
      • 2021-02-06
      • 2011-03-27
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      相关资源
      最近更新 更多