如何在 INSERT INTO SELECT 语句中使用 REGEXP 仅在插入前复制数值?
How to use REGEXP in INSERT INTO SELECT statement to copy number values only before insert?
我已插入 select 语句以将值从一个 table 复制到另一个。
两个 table 都有手机专栏,但在来源 table 中,手机号码附有如下文字: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 会做错什么?谢谢
您尝试过使用 REPLACE
INSERT INTO employee (employee_number, mobile)
SELECT eb.employee_number,REPLACE(eb.mobile,'Primary contact : ','')
FROM employee_bulkadd_staging eb
SELECT SUBSTRING( mobile FROM INSTR(mobile, ':')+1)
FROM employee_bulkadd_staging
您可以使用:
select REGEXP_SUBSTR(col1,"[0-9]+") as col1 from test;
测试示例:
create table test (
col1 varchar(255));
insert into test values
('Primary contact : 1234567890.'),
('1');
Result:
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
我已插入 select 语句以将值从一个 table 复制到另一个。
两个 table 都有手机专栏,但在来源 table 中,手机号码附有如下文字: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 会做错什么?谢谢
您尝试过使用 REPLACE
INSERT INTO employee (employee_number, mobile)
SELECT eb.employee_number,REPLACE(eb.mobile,'Primary contact : ','')
FROM employee_bulkadd_staging eb
SELECT SUBSTRING( mobile FROM INSTR(mobile, ':')+1)
FROM employee_bulkadd_staging
您可以使用:
select REGEXP_SUBSTR(col1,"[0-9]+") as col1 from test;
测试示例:
create table test (
col1 varchar(255));
insert into test values
('Primary contact : 1234567890.'),
('1');
Result:
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