【发布时间】:2021-04-06 20:23:37
【问题描述】:
我有一个包含 253 个字符的字符串,但由于某些字符长于一个字节(在这种情况下,它包含几个零宽度空格),它占用了 261 个字节。
我想将此字符串插入到utf8mb4 表中的VARCHAR(255) 列中,但它会引发此错误:
INSERT INTO mytable(mycolumn) VALUES("This string contains 253 characters which should fit inside of a varchar(255) column, but because of the zero width spaces, it takes up more than 255 bytes and therefore throws an error when you try to insert it into the table and that makes me sad.")
/*Error 1406: Data too long for column*/
这是两个长度函数输出的示例:
/* Returns 253 */
SELECT CHAR_LENGTH("This string contains 253 characters which should fit inside of a varchar(255) column, but because of the zero width spaces, it takes up more than 255 bytes and therefore throws an error when you try to insert it into the table and that makes me sad.");
/* Returns 261 */
SELECT LENGTH("This string contains 253 characters which should fit inside of a varchar(255) column, but because of the zero width spaces, it takes up more than 255 bytes and therefore throws an error when you try to insert it into the table and that makes me sad.");
要解决这个问题,我需要剪掉足够的字符以使字符串在字节限制内。
不幸的是,LEFT() 函数似乎不适合这个:
/* Still returns 261 because it counts characters, not bytes */
SELECT LENGTH(LEFT("This string contains 253 characters which should fit inside of a varchar(255) column, but because of the zero width spaces, it takes up more than 255 bytes and therefore throws an error when you try to insert it into the table and that makes me sad.", 255));
那么,我该如何修剪字符串呢?
【问题讨论】:
-
这能回答你的问题吗? Replace NonASCII Characters in MYSQL
-
@P.Salmon - 不,这是关于替换字符的。我想保留零宽度字符,但修剪字符串的末尾,直到它是可接受的长度。
标签: mysql substring trim utf8mb4