mysql - Selecting strings from a column where some are utf8 encoded and others are not
问题描述
The data is the names of country subdivisions. Some have been stored as utf8 and some are not. example, this is how they are in my table:
statename
Bocas del Toro
ChiriquÃ
Coclé
Colón
Darién
Veraguas
Panamá Oeste
Emberá
Kuna Yala
Ngöbe-Buglé
This question/answer gets me really close to a solution: How to fix double-encoded UTF8 characters (in an utf-8 table)
If I use:
CONVERT(CAST(CONVERT(statename USING latin1) AS BINARY) USING utf8)
:
statename
Bocas del Toro
Chiriquí
Coclé
Col
Dari
Veraguas
Panam
Emberá
Kuna Yala
Ng
the characters stored as "é" for example, just end the string.
the variation provided in that answer ,
SELECT CASE
WHEN CONVERT( CAST( CONVERT( statename USING latin1 ) AS BINARY ) USING utf8 ) IS NULL
THEN statename
ELSE CONVERT( CAST( CONVERT( statename USING latin1 ) AS BINARY ) USING utf8 )
END
FROM
returned the same result, though I am not even sure i implemented it correctly in this select.
I am not permitted to normalize this data in this case, so I would like to select it and get
Bocas del Toro
Chiriquí
Coclé
Colón
Darién
Veraguas
Panamá Oeste
Emberá
Kuna Yala
Ngöbe-Buglé
Will this be possible?
解决方案
这似乎是SQL_MODE
. 为了使转换失败并返回NULL
-STRICT_TRANS_TABLES
必须设置模式。你可以设置它
SET SESSION sql_mode = CONCAT('STRICT_TRANS_TABLES,', @@sql_mode);
如果您不想在同一会话中中断其他“工作”查询,则应在获得结果后将其重置:
SET @old_sql_mode = @@sql_mode;
SET SESSION sql_mode = CONCAT('STRICT_TRANS_TABLES,', @@sql_mode);
SELECT COALESCE(
CONVERT( CAST( CONVERT( statename USING latin1 ) AS BINARY ) USING utf8 ), statename
) as statename
FROM yourTable;
SET SESSION sql_mode = @old_sql_mode;
注意:我已将您的查询更改为使用COALESCE()
而不是CASE
语句,因此您不需要复制转换代码。
推荐阅读
- neo4j - 是否可以将 Lucene 评分作为 0 到 1 之间的数字?
- ios - 如何检查应用程序在发布模式下是否运行良好?
- python - 如何找到迭代两个数组的时间?
- selenium-webdriver - 如何处理硒中网页的自动重定向
- linkedin - 用于检索消息的 LinkedIn API?
- pine-script - 职位管理策略?获利一半
- apache-spark - Spark 阶段花费的时间太长 - 2 个执行者完成“所有”工作
- python - 使用 Robot Framework 4.0 是否可以将值从 .body 输入和输出到 python 脚本和从 python 脚本到 .body 测试套件?
- pandas - 仅爬取第一页并将详细内容保存为 Python 中的数据框
- azure-ad-graph-api - 如何使用 Microsoft Graph API 阅读托管在本地 Exchange 服务器上的 Outlook 电子邮件?