将最后几个单词移到前面的同一列

时间:2016-04-06 19:47:10

标签: 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%';

1 个答案:

答案 0 :(得分:2)

这是Oracle PL / SQL吗?在这种情况下,您可以使用regexp来交换这两个部分(但不能使用最大的perf):

与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%';

希望这有任何帮助。