![](/img/trans.png)
[英]select concat(first_name,' ',last_name) as "full name "from employees ---how to bring space when 2 columns are concatenated in oracle---
[英]Extracting First_name & Last_name from email oracle using Reg_exp
如何使用 oracle REGEXP_SUBSTR 從電子郵件中提取名字和姓氏,
電子郵件: susan.ryan@email.com
預期輸出:
名 | 姓 |
---|---|
蘇珊 | 瑞安 |
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;
但我得到的結果是
名 | 姓 |
---|---|
蘇珊 | 瑞安@電子郵件。 |
你有
substr(email, instr(email, '.') + 1, instr(email, '@')) as last_name
但是第二個參數不是結束位置,而是請求的長度,所以必須減去點的位置:
substr(email, instr(email, '.') + 1, instr(email, '@') - instr(email, '.') - 1) as last_name
順便說一下,使用REGEXP_SUBSTR
更容易:
regexp_substr(email, '[[:alpha:]]+', 1, 1) as first_name,
regexp_substr(email, '[[:alpha:]]+', 1, 2) as last_name
我們在這里尋找僅由電子郵件中的字母組成的子字符串。 對於 first_name 我們取第一個這樣的字符串,對於 last_name 取第二個。 這當然依賴於您表中的所有電子郵件均由 firstname.lastname@domain 組成。
這是REGEXP_SUBSTR
上的文檔: https : 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.