mysql - MySQL如何拆分字段数据中的字符串

标签 mysql sql sql-update

以语言环境为例(id-id)

我想要更新此区域设置的查询

我需要如何拆分字符串连字符应更改为下划线,最后 2 位数字应为大写

o/p 应该是:id_ID

我已经尝试过这个查询

UPDATE  substring_index(locale,'-',1)=substring_index(locale,'-',1),
locale=REPLACE(locale,'-','_'),
substring_index(locale,'-',-1)=UPPER(substring_index(locale,'-',-1))
jlg_language_code_mapping;

请大家帮帮我..!

enter image description here

最佳答案

这里有一个选项:

SQL> with test (id, locale) as
  2    (select 'arabic', 'ar'                  from dual union all
  3     select 'indonesian', 'id-id'           from dual union all
  4     select 'malay', 'ms-my'                from dual union all
  5     select 'bulgarian', 'bg'               from dual union all
  6     select 'chinese (simplified)', 'zh-cn' from dual
  7    )
  8  select id, locale,
  9    regexp_substr(locale, '^\w+') ||
 10    replace(upper(regexp_substr(locale, '-\w+$')), '-', '_') new_locale
 11  from test;

ID                   LOCALE                    NEW_LOCALE
-------------------- ------------------------- -------------------------
arabic               ar                        ar
indonesian           id-id                     id_ID
malay                ms-my                     ms_MY
bulgarian            bg                        bg
chinese (simplified) zh-cn                     zh_CN

SQL>

更新:

update your_table set
  locale = regexp_substr(locale, '^\w+') ||
           replace(upper(regexp_substr(locale, '-\w+$')), '-', '_');

关于mysql - MySQL如何拆分字段数据中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58437752/

相关文章:

mysql - 在 MySQL 中选择更新表数据

mysql - 使用另一个表中的列更新一个表中的时间戳列

mysql - Sql Count 包含一些值的行

php - jquery自动完成将不会显示数据

mysql - 更新 sql 查询 where select max

c# - 如何在 .NET Core 1.0 中使用 Entity Framework 运行 sql 脚本?

php - 使用 Where 子句在数据库列中查找最大值

mysql - 工作台 - 错误 : aggregates not allowed in GROUP BY clause

c# - MySQL 站点搜索中参数化查询的问题

php - 对多个行/列使用一个 UPDATE 查询