简体   繁体   English

使用 Reg_exp 从电子邮件预言机中提取名字和姓氏

[英]Extracting First_name & Last_name from email oracle using Reg_exp

How to extract First_name & last_name from email using oracle REGEXP_SUBSTR,如何使用 oracle REGEXP_SUBSTR 从电子邮件中提取名字和姓氏,

Email: susan.ryan@email.com电子邮件: susan.ryan@email.com

Expected output:预期输出:

First_name Last_name
susan苏珊 ryan瑞安
select
  substr('susan.ryan@email.com',1,(INSTR('susan.ryan@email.com','.')-1)) first_name,
  substr('susan.ryan@email.com',(INSTR('susan.ryan@email.com','.')+1),(INSTR('susan.ryan@email.com','@'))) last_name
from dual;

But i'm getting result as但我得到的结果是

first_name Last_name
susan苏珊 ryan@email.瑞安@电子邮件。

You have你有

substr(email, instr(email, '.') + 1, instr(email, '@')) as last_name

But the second parameter is not the end position, but the requested length, so you must subtract the position of the dot:但是第二个参数不是结束位置,而是请求的长度,所以必须减去点的位置:

substr(email, instr(email, '.') + 1, instr(email, '@') - instr(email, '.') - 1) as last_name

That's easier with REGEXP_SUBSTR by the way:顺便说一下,使用REGEXP_SUBSTR更容易:

regexp_substr(email, '[[:alpha:]]+', 1, 1) as first_name,
regexp_substr(email, '[[:alpha:]]+', 1, 2) as last_name

We are looking for substrings that only consist of letters in the e-mail here.我们在这里寻找仅由电子邮件中的字母组成的子字符串。 For first_name we are taking the first such string, for last_name the second one.对于 first_name 我们取第一个这样的字符串,对于 last_name 取第二个。 This relies of course on all e-mails in your table equally consisting of firstname.lastname@domain.这当然依赖于您表中的所有电子邮件均由 firstname.lastname@domain 组成。

Here is the docs on REGEXP_SUBSTR : https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/REGEXP_SUBSTR.html#GUID-2903904D-455F-4839-A8B2-1731EF4BD099这是REGEXP_SUBSTR上的文档: https : REGEXP_SUBSTR

Here are some examples of how to do this.以下是有关如何执行此操作的一些示例。 First remove the domain using SUBSTR (column "domain") or REGEXP (column "domain_regexp"), then split the portion before the domain (column "no_domain") using REGEXP_SUBSTR :首先使用SUBSTR (列“域”)或REGEXP (列“domain_regexp”)删除域,然后使用REGEXP_SUBSTR拆分域(列“no_domain”)之前的部分:

WITH samples AS
(
  SELECT '-susan.ryan@email.com' as str FROM DUAL UNION
  SELECT 'roger@email.com' as str FROM DUAL
)
SELECT 
str as email,
REGEXP_SUBSTR(str,'@.+$') AS domain_regexp,
SUBSTR(str, INSTR(str,'@')) as domain,
SUBSTR(str, 1, INSTR(str,'@') - 1) as no_domain,
REGEXP_SUBSTR(SUBSTR(str, 1, INSTR(str,'@') - 1),'[^.]+',1,1) AS first_name,
REGEXP_SUBSTR(SUBSTR(str, 1, INSTR(str,'@') - 1),'[^.]+',1,2) AS last_name
from samples;

EMAIL                 DOMAIN_REGEXP         DOMAIN                NO_DOMAIN             FIRST_NAME            LAST_NAME            
--------------------- --------------------- --------------------- --------------------- --------------------- ---------------------
-susan.ryan@email.com @email.com            @email.com            -susan.ryan           -susan                ryan                 
roger@email.com       @email.com            @email.com            roger                 roger                                      


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择 concat(first_name,' ',last_name) 作为员工的“全名”---在 oracle 中连接 2 列时如何带空间--- - select concat(first_name,' ',last_name) as "full name "from employees ---how to bring space when 2 columns are concatenated in oracle--- 通过First_Name和/或Last_Name查询sql表 - query sql table By First_Name and/or Last_Name 如何使用sql查询组合first_name和last_name? - How to combined first_name and last_name using sql query? 查询以使用外键获取行值(first_name 和 last_name) - Query to fetch row values (first_name & last_name) using foreign key 试图在 Oracle SQL 中将 FULL_NAME 分成 FIRST_NAME 和 LAST_NAME,但它没有显示表本身的任何更改 - Trying to separate a FULL_NAME into FIRST_NAME and LAST_NAME in Oracle SQL, but it does not show any changes on the table itself 如有空格,如何将NAME分为3个不同的字段FIRST_NAME,MIDDLE_NAME和LAST_NAME - How to split NAME into 3 different fields FIRST_NAME, MIDDLE_NAME & LAST_NAME when there is a space 表示 Last_Name + First_Name 有一个具有特定值的记录 - Indicate a Last_Name + First_Name has one record with specific value 在本地数据库中存储 OAuth 用户信息(用户名、名字、姓氏等) - Storing OAuth user info (username, first_name, last_name etc) in local database 具有名称,名字和姓氏的PostgreSQL多表查询 - PostgreSQL multi-table query with name, first_name, and last_name 将 first_name 和 last_name 列中的名称截断为 1 个字符的最有效方法是什么? - What's the most efficient way of truncating the names in a first_name and last_name column to 1 character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM