Reputation: 239
I want to do a SELECT statement where I replace every (") with a (\") in multiple columns. Now how do I do that?
Is it possible to target more than one column with the REPLACE function within a SELECT statement?
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.
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]
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:
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:
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!
Upvotes: 4
Views: 4468
Reputation: 1095
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;
/
Upvotes: 0
Reputation: 7568
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.
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.
Upvotes: 6