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!
I think your only sensible option is to use replace on every column individually.
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.