简体   繁体   English

如何在SQL中的SELECT语句中替换多列中的相同内容?

[英]How do I replace the same thing in multiple columns within a SELECT statement in SQL?

I want to do a SELECT statement where I replace every (") with a (\\") in multiple columns . 我想做一个SELECT语句,我在多列中用(\\ n)替换每个(“)。 Now how do I do that? 现在我该怎么做?

Is it possible to target more than one column with the REPLACE function within a SELECT statement? 是否可以在SELECT语句中使用REPLACE函数定位多个列?

I know this is invalid code but just to visualize what I mean: 我知道这是无效的代码,但只是想象我的意思:

SELECT REPLACE(firstColumn, anotherColumn, '"', '\"') 
FROM testTable

that of course does not work because the REPLACE function expects only three parameters . 这当然不起作用,因为REPLACE函数只需要三个参数

I really need help with this as I couldn't find anything that worked for me yet. 我真的需要帮助,因为我找不到任何对我有用的东西。

thanks in advance! 提前致谢!

[edit - 24.04.2012 - 16:20 UTC+01:00] [编辑 - 24.04.2012 - 16:20 UTC + 01:00]

For those who might have a similar problem, this is my solution : I used regular expression to build my SELECT REPLACE statement which looks like this: 对于那些可能有类似问题的人,这是我的解决方案 :我使用正则表达式来构建我的SELECT REPLACE语句,如下所示:

Regex: {(^[^, ]+,)}|( {[^, ]+,})|( {[^, ]+$})

In order to use this you need all your columns you want to go through each seperated with a comma and a space like this: 为了使用它,你需要你想要通过逗号和空格分隔的所有列,如下所示:

ID, Gender, Firstname, Lastname, Street, Zip, City, EMail, Age,

and then you need a tool (in my case Visual Studio) that is capable of replacing with tagged expressions (between the {} brackets in regex) and replace it with something like this: 然后你需要一个工具(在我的情况下是Visual Studio),它能够替换为带标记的表达式(在正则表达式中的{}括号之间),并用以下内容替换它:

REPLACE(\\1\\2\\3 'replaceMe', 'withMe') AS \\1\\2\\3

Hope this helps anyone who might have a similar problem. 希望这可以帮助任何可能遇到类似问题的人。 It is probably not the best solution but it worked for me. 它可能不是最好的解决方案,但它对我有用。 Thanks for the quick help! 感谢您的快速帮助!

I think your only sensible option is to use replace on every column individually. 我认为你唯一明智的选择就是在每一列上单独使用替换。

SELECT
    REPLACE(firstColumn, '"', '\"'),
    REPLACE(secondColumn, '"', '\"')
FROM testTable

You could possibly do some clever pivoting of the data in order to only apply the REPLACE once but I don't see the benefit. 您可以对数据进行一些巧妙的旋转,以便仅应用REPLACE一次,但我没有看到好处。

If the actual replace you are doing is very complex then you could write a function to do it to save code clutter and duplication. 如果您正在进行的实际替换非常复杂,那么您可以编写一个函数来执行此操作以节省代码混乱和重复。

It seems like maybe it's more than you were looking to do, but something like the following should work: 看起来它可能比你想做的更多,但是类似下面的东西应该有效:

declare 
  qrystart varchar2(15) := 'select replace(';
  qryend varchar2(31) := ', ''"'', ''\"'') from <your_table_name>';
  columnname varchar2(20);
begin 
  for columnItem in (
    select column_name from all_tab_columns
    where lower(table_name) = '<your_table_name>')
  loop
  columnname := columnitem.column_name;
    execute immediate qrystart || columnname || qryend;
  end loop;
end;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM