Mysql TRIM/RTRIM/REPLACE 无法从电子邮件列中删除尾随空格
Mysql TRIM/RTRIM/REPLACE doesn't work to remove trailing spaces from an email column
我有这个 insert select 语句,从员工批量复制数据添加暂存到员工 table:
INSERT INTO employee (tr_email)
SELECT TRIM(eb.tr_email)
FROM employee_bulkadd_staging eb
ON DUPLICATE KEY UPDATE
tr_email=(SELECT IF(eb.tr_email = '' or eb.tr_email IS NULL, employee.tr_email,TRIM(eb.tr_email)));
即使我将其更改为 RTRIM 或 REPLACE(eb.tr_email, ' ', '') 以将空格替换为空,它仍然不会修改要插入的数据。我无法登录,因为我的后端应用程序检测到电子邮件地址末尾的这些尾随空格。
带有空格的示例电子邮件值如下所示,突出显示以显示空格:
来自来源table的电子邮件地址列,数据类型为varchar(100) utf8_general_ci
而目标 table 电子邮件列是 varchar(100) latin1_swedish_ci
我正在使用 MySQL 5. 感谢您的帮助。
我认为由于文本中的 CHAR(10),您遇到了这个问题。你能尝试删除它吗 -
REPLACE(eb.tr_email, CHAR(10), '')
我已经对您的数据进行了编码,但无法重现空格问题。
Ankit 建议,回车 return 或换行符是否有问题:char(11) and/or char(13)?我添加了标记 <
和 >
以使空白字符的位置更清楚。
CREATE TABLE employee (
id int not null auto_increment primary key,
tr_email varchar(100)
collate latin1_swedish_ci
)
;
CREATE TABLE employee_bulkadd_staging(
id int not null auto_increment primary key,
tr_email varchar(100)
collate utf8_general_ci
)
;
insert into employee_bulkadd_staging (tr_email)
values ('name@domain.com
');
insert into employee (tr_email)
select trim(REPLACE(tr_email,'
','')) from employee_bulkadd_staging
INSERT INTO employee (tr_email)
SELECT TRIM(eb.tr_email)
FROM employee_bulkadd_staging eb
ON DUPLICATE KEY UPDATE
tr_email=(SELECT IF(eb.tr_email = '' or eb.tr_email IS NULL, employee.tr_email,TRIM(eb.tr_email)))
select
concat('<',tr_email,'>' )
,
REPLACE(concat('<',tr_email,'>' ), '
', '')r
from employee_bulkadd_staging eb
concat('<',tr_email,'>' ) | r
:------------------------ | :-------------------
<name@domain.com <br>> | <name@domain.com >
select id,concat('<',tr_email,'>') ebs from
employee_bulkadd_staging;
select id, concat('<',tr_email,'>' ) emp
from employee;
id | ebs
-: | :-----------------------
1 | <name@domain.com <br>>
id | emp
-: | :-----------------------
1 | <name@domain.com>
2 | <name@domain.com <br>>
db<>fiddle here
我有这个 insert select 语句,从员工批量复制数据添加暂存到员工 table:
INSERT INTO employee (tr_email)
SELECT TRIM(eb.tr_email)
FROM employee_bulkadd_staging eb
ON DUPLICATE KEY UPDATE
tr_email=(SELECT IF(eb.tr_email = '' or eb.tr_email IS NULL, employee.tr_email,TRIM(eb.tr_email)));
即使我将其更改为 RTRIM 或 REPLACE(eb.tr_email, ' ', '') 以将空格替换为空,它仍然不会修改要插入的数据。我无法登录,因为我的后端应用程序检测到电子邮件地址末尾的这些尾随空格。
带有空格的示例电子邮件值如下所示,突出显示以显示空格:
来自来源table的电子邮件地址列,数据类型为varchar(100) utf8_general_ci
而目标 table 电子邮件列是 varchar(100) latin1_swedish_ci
我正在使用 MySQL 5. 感谢您的帮助。
我认为由于文本中的 CHAR(10),您遇到了这个问题。你能尝试删除它吗 -
REPLACE(eb.tr_email, CHAR(10), '')
我已经对您的数据进行了编码,但无法重现空格问题。
Ankit 建议,回车 return 或换行符是否有问题:char(11) and/or char(13)?我添加了标记 <
和 >
以使空白字符的位置更清楚。
CREATE TABLE employee ( id int not null auto_increment primary key, tr_email varchar(100) collate latin1_swedish_ci ) ; CREATE TABLE employee_bulkadd_staging( id int not null auto_increment primary key, tr_email varchar(100) collate utf8_general_ci ) ;
insert into employee_bulkadd_staging (tr_email) values ('name@domain.com ');
insert into employee (tr_email) select trim(REPLACE(tr_email,' ','')) from employee_bulkadd_staging
INSERT INTO employee (tr_email) SELECT TRIM(eb.tr_email) FROM employee_bulkadd_staging eb ON DUPLICATE KEY UPDATE tr_email=(SELECT IF(eb.tr_email = '' or eb.tr_email IS NULL, employee.tr_email,TRIM(eb.tr_email)))
select concat('<',tr_email,'>' ) , REPLACE(concat('<',tr_email,'>' ), ' ', '')r from employee_bulkadd_staging eb
concat('<',tr_email,'>' ) | r :------------------------ | :------------------- <name@domain.com <br>> | <name@domain.com >
select id,concat('<',tr_email,'>') ebs from employee_bulkadd_staging; select id, concat('<',tr_email,'>' ) emp from employee;
id | ebs -: | :----------------------- 1 | <name@domain.com <br>> id | emp -: | :----------------------- 1 | <name@domain.com> 2 | <name@domain.com <br>>
db<>fiddle here