mysql - 将最后几个单词移至同一列的前面

标签 mysql plsql

我正在尝试创建一个命令,该命令将从列中获取最后几个单词并将其添加到前面(并删除逗号),例如:

Update:
Engineering and Applied Science (SEAS), The Fu Foundation School of


To:
The Fu Foundation School of Engineering and Applied Science (SEAS)

到目前为止我已经有了

update ONLINE_GIFT_DIVISIONS
set test_newgiving_name = case
                    when DIVISION_TITLE LIKE '%, School of%' then
                     ''
                    when DIVISION_TITLE LIKE '%, Graduate School of%' then
                     ''
                    when DIVISION_TITLE LIKE '%, Mailman School of%' then
                     ''
                    when DIVISION_TITLE LIKE '%, The Fu Foundation School of%' then
                     ''
                  end
where division_title like '%, School of%'
where division_title like '%, Graduate School of%'
where division_title like '%, Mailman School of%'
where division_title like '%, The Fu Foundation School of%';

最佳答案

这是 Oracle PL/SQL 吗?在这种情况下,您可以使用正则表达式来交换两个部分(但不是最好的性能):

使用 Oracle REGEXP_REPLACE:

你有 2 个被 () 包围的元素,它们可以是任何东西 -> .*,用逗号分隔:这给你一个像这样的模式: (.*),(.*)。然后,替换中的第一个元素由 \1 寻址,第二个元素由 \2 寻址 -> 替换字符串将变为 \2\1

SELECT 
  regexp_replace(str_var, '(.*),(.*)', '\2 \1'), str_var  FROM 
      (
           select 'Engineering and Applied Science (SEAS), The Fu Foundation School of' str_var from dual
union all  select 'ISIMA the best, Mailman School of'                                           from dual
union all  select 'another text, splitted with comma, but if there is another ? '               from dual
  ) ;

用它来更新你的表:

update ONLINE_GIFT_DIVISIONS
  set test_newgiving_name = regexp_replace(DIVISION_TITLE, '(.*),(.*)' , '\2 \1')  
where division_title like '%, School of%'
   or division_title like '%, Graduate School of%'
   or division_title like '%, Mailman School of%'
   or division_title like '%, The Fu Foundation School of%';

希望这对您有所帮助。

关于mysql - 将最后几个单词移至同一列的前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36460984/

相关文章:

sql - PL/SQL 添加多列查询单函数

Oracle pl/sql ORA-00904 标识符无效

sql - 'like' 和困惑的 MySQL 查询问题

oracle - pl/sql 存储过程 : parameter name same as column name

xml - PL/SQL : Count number of nodes in xml

PHP - MySQL 查询拒绝执行

oracle - Postgresql 存储过程中基于 session 的全局变量?

mysql - 返回mysql查询中两个表的计数数据

mysql - 计算超过和低于阈值的项目数

mysql - Node JS MySQL 查询依赖于另一个查询