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

时间:2019-04-20 15:30:12

标签: sql oracle regexp-substr

我有一个分配,其中有一个“供应商”表,每个供应商都附带说明。有些说明提供了电子邮件地址,而其他说明则没有。我只需要显示提供的电子邮件,如果没有,则需要显示“没有提供电子邮件”。我很容易提取电子邮件地址,但是我不知道如何用请求的消息替换所有空的插槽。

我尝试了几种可能很错误的方法:

尝试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上查找措词的变化形式,而我所读的教科书并未明确说明这种特殊情况。

这是我使用的表格,用于显示有关结果的上下文:

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

1 个答案:

答案 0 :(得分:3)

您需要使用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,则返回它;否则,它将返回第二个参数。

COALESCENVL类似,但是它接受您希望提供的任意数量的参数,并返回第一个非NULL的参数。如果所有参数均为NULL,COALESCE将返回NULL。

好运。