如何在字段数据MySQL中拆分字符串

时间:2019-10-17 17:18:10

标签: mysql sql sql-update

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

我要更新查询此语言环境

我如何分割字符串连字符应更改为下划线,后两位数字应为大写

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

1 个答案:

答案 0 :(得分:2)

这里是一个选择:

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+$')), '-', '_');
相关问题