简体   繁体   English

Mysql TRIM/RTRIM/REPLACE 无法从 email 列中删除尾随空格

[英]Mysql TRIM/RTRIM/REPLACE doesn't work to remove trailing spaces from an email column

I have this insert select statement, copying data from employee bulk add staging to employee table:我有这个 insert select 语句,将数据从员工批量添加到员工表中:

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)));

Even if I change it to RTRIM or REPLACE(eb.tr_email, ' ', '') to replace the spaces with nothing it will still not modify the data to be inserted.即使我将其更改为 RTRIM 或 REPLACE(eb.tr_email, ' ', '') 以将空格替换为空,它仍然不会修改要插入的数据。 I cannot get my login to work since my backend application detects these trailing white spaces at the end of the email address.我无法登录,因为我的后端应用程序在 email 地址末尾检测到这些尾随空格。

Sample email value with spaces is like this, highlighted to show the spaces:带有空格的示例 email 值如下所示,突出显示以显示空格:

在此处输入图像描述

The email address column from the source table, the data type is varchar(100) utf8_general_ci来自源表的email地址列,数据类型为varchar(100) utf8_general_ci

while the target table the email column is varchar(100) latin1_swedish_ci而目标表 email 列是varchar(100) latin1_swedish_ci latin1_swedish_ci

I am using MySQL 5. Thanks of any help.我正在使用 MySQL 5. 感谢您的帮助。

I think you are facing this problem due to CHAR(10) in your text.我认为由于文本中的 CHAR(10),您正面临此问题。 Can you try removing it -你能尝试删除它吗 -

REPLACE(eb.tr_email, CHAR(10), '')

I've coded your data and can't reproduce the problem of spaces.我已经对您的数据进行了编码,但无法重现空格问题。
Is it a problem with a carriage return or newline character: char(11) and/or char(13) as Ankit suggests?回车符或换行符是否有问题:如 Ankit 所建议的 char(11) 和/或 char(13)? I've added markers < and > to make it clearer where there are whitespace characters.我添加了标记<>以使空白字符的位置更清楚。

 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,'>' ) |连接('<',tr_email,'>')| r:------------------------ |:------------------- <name@domain.com <br>> | r:------------------------ |:---------------- <名称@域名.com <br>> | <name@domain.com > <名称@域.com>
 select id,concat('<',tr_email,'>') ebs from employee_bulkadd_staging; select id, concat('<',tr_email,'>' ) emp from employee;
 id |编号 | ebs -: |:----------------------- 1 | ebs -: |:------------------------ 1 | <name@domain.com <br>> id | <name@domain.com <br>> ID | emp -: |:----------------------- 1 | emp -: |:------------------------ 1 | <name@domain.com> 2 | <name@domain.com> 2 | <name@domain.com <br>> <名称@域.com <br>>

db<>fiddle here db<> 在这里摆弄

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM