我已插入 select 陳述句以將值從一個表復制到另一個表。
兩個表中都有一個 mobile 列,但在源表中,手機號碼附加了一個文本,如下所示:Primary contact : 1234567890。
所以我只想復制手機的 1234567890 部分。
我已經使用帶有 REGEXP 的子查詢完成了這項作業:
INSERT INTO employee (employee_number, mobile)
SELECT eb.employee_number,(SELECT mobile FROM employee_bulkadd_staging WHERE mobile REGEXP '^[0-9] $')
FROM employee_bulkadd_staging eb
但它會在我的移動列中插入 NULL 值。
我也這樣做了:
INSERT INTO employee (employee_number, mobile)
SELECT eb.employee_number,eb.mobile REGEXP '^[0-9] $'
FROM employee_bulkadd_staging eb
但這會插入 0 作為值。
請讓我知道最好的方法是什么。或者我使用 REGEXP 會做錯什么?謝謝。
uj5u.com熱心網友回復:
你可以做INSTR()和SUBSTR()
SELECT SUBSTRING( mobile FROM INSTR(mobile, ':') 1)
FROM employee_bulkadd_staging
uj5u.com熱心網友回復:
您可以使用:
select REGEXP_SUBSTR(col1,"[0-9] ") as col1 from test;
測驗示例:
create table test (
col1 varchar(255));
insert into test values
('Primary contact : 1234567890.'),
('1');
結果:
amount 1234567890 1
在你的情況下:
INSERT INTO employee ( employee_number,
mobile
)
SELECT eb.employee_number,
( SELECT mobile
FROM employee_bulkadd_staging
WHERE mobile REGEXP_SUBSTR(col1,"[0-9] ")
)
FROM employee_bulkadd_staging eb
uj5u.com熱心網友回復:
你試過用REPLACE
INSERT INTO employee (employee_number, mobile)
SELECT eb.employee_number,REPLACE(eb.mobile,'Primary contact : ','')
FROM employee_bulkadd_staging eb
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/458783.html
下一篇:浮點、范圍和零的正則運算式
