如何在 Oracle 中使用 SQL 中的 REGEXP_SUBSTR 将空槽替换为自定义消息

How to replace empty slots with a custom message using REGEXP_SUBSTR in SQL in Oracle

我有一个作业,我有一个 table 的 "suppliers",每个供应商都有一个描述。一些描述提供了电子邮件地址,而其余的则没有。我只需要显示提供的电子邮件,如果没有,我需要显示 "No Email Provided"。我很容易就能提取电子邮件地址,但我不知道如何用请求的消息替换所有空槽。

我试过几种方法,可能都错了:

尝试 1:

SELECT REGEXP_SUBSTR(description, '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}') OR 'No email provided' "Supplier emails" FROM suppliers;

尝试 2:(注意:我预计这不会正常工作,但由于缺乏其他想法,还是决定试一试)

SELECT REGEXP_SUBSTR(description, '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}') || 'No email provided' "Supplier emails" FROM suppliers;

尝试 3:

SELECT REGEXP_SUBSTR(description, '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}', 'No email provided') "Supplier emails" FROM suppliers;

尝试 4:

SELECT REGEXP_SUBSTR(description, '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}') "Supplier emails" FROM suppliers;
SELECT REPLACE('-', "No Email Provided") FROM suppliers;

我试过在 Google 上查找,但措辞有所不同,我的教科书并没有清楚地解释这种具体情况。

这是我正在使用的 table,关于结果的上下文:

INSERT INTO suppliers
  VALUES ('Second Hand Reads', 
         'wholesaler of used books only, located in Chicago, 634.555-8787');
INSERT INTO suppliers
  VALUES ('Leftovers', 
     'Physical store location, located in Seattle, willing to fill referred sales, sales@leftovers.com');
INSERT INTO suppliers
  VALUES ('Read Again', 
 'Chain of used book store fronts, seeking online sales partner, located in western U.S., 919-555-3333');
INSERT INTO suppliers
  VALUES ('Bind Savers', 'Used book wholsaler, stock includes international titles, 402-555-2323');
INSERT INTO suppliers
  VALUES ('Book Recyclers', 'Used book chain, located in Canada, large volume of sales, 888.555.5204');
INSERT INTO suppliers
  VALUES ('Page Shock', 'Book wholsaler for specialty books and graphic novels, help@pageshock.com');
INSERT INTO suppliers
  VALUES ('RePage', 'Used book vendor, only wholesales, Wash D.C., 555-0122');
COMMIT;

预期输出是

                           Supplier emails
------------------------------------------------------------------
No email provided
--------------------
sales@leftovers.com
--------------------
No email provided
--------------------
No email provided
--------------------
No email provided
--------------------
help@pageshock.com
--------------------
No email provided

但输出要么是一个错误,要么是我预期的错误

                           Supplier emails
------------------------------------------------------------------
No email provided
-------------------------------------
sales@leftovers.comNo email provided
-------------------------------------
No email provided
-------------------------------------
No email provided
-------------------------------------
No email provided
-------------------------------------
help@pageshock.comNo email provided
-------------------------------------
No email provided

您需要使用 NVLCOALESCE 函数:

SELECT STORE_NAME,
       DESCRIPTION,
       NVL(REGEXP_SUBSTR(description, '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}'),
           'No email provided') AS EMAIL
  FROM SUPPLIERS

dbfiddle here

NVL 接受两个参数。如果第一个参数不为 NULL 则 returns 即可;否则它 return 是第二个参数。

COALESCENVL 类似,但它接受您提供的任意数量的参数,并且 return 是第一个非 NULL 的参数。如果所有参数都是 NULL COALESCE 将 return NULL.

祝你好运。