All,
I am trying to clean up data in a SQLite database, that contains tons of escaped single and double quotes.
Example: \"Text\" has \'junk\' like this all over the place.
When I do a SELECT to see if the REPLACE function has the correct pattern, the REPLACE is not working:
-- replace escaped single quotes
SELECT id,subject,body,replace(body,"\\'","'") as `clean-body`
FROM article
WHERE id=1118
Thanks for looking
There is no need to escape the backslash. Try this in place of your original call to replace:
replace(body,"\'","'")
Related
I'm trying to match a certain text that includes a single quote (i.e. 'company's report...')
normally I would have used the E' literal + ' or double single quotes.
but when it gets to using the LIKE '%' operator, things got complicated.
what is the best approach to match a text with a single quote?
You can escape single quote with another single quote. Example:
WHERE column LIKE 'RSNboim''s'
From https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
To include a single-quote character within a string constant, write two adjacent single quotes, e.g., 'Dianne''s horse'. Note that this is not the same as a double-quote character (").
You can use Dollar-quoted String Constants at Lexical Structure
Your condition should be something like below;
select * from atable
where afield like $$Dianne's %$$
I want to replace all instances of a string quote (') with two single quotes ('') in a string.
Lets say e'QmfgLu/]sf]/sd is a string and I want to replace ' with ''.
The result must be e''OmfgLu/]
I tried this query:
update customer set name=REGEXP_REPLACE(name, E'\'', '''');
also
update customer set name=REPLACE(name, E'\'', '''');
This query is not properly working. What is the suitable way to write the query?
You may replace a single occurrence of single quotes with 2 quotes using this regexp.
update customer set name=REGEXP_REPLACE(name, $$([^'])'([^'])$$, $$\1''\2$$ ,'g');
$$([^'])'([^'])$$ - represents a sequence of any character other than a single quote followed by a quote and then a non-quote character.
I'm using the dollar quoting to avoid confusing quotes.
Demo
EDIT
As #edruid pointed out, to handle case for quotes at the start and end of string, use:
REGEXP_REPLACE(name, $$([^']|^)'(?!')$$, $$\1''$$ ,'g')
This uses a negative lookahead for matching a single quote - (?!')
Demo2
In postgres the way to have a single quote in a string is to type '' (' is used as the escape character) so your replace would be
update customer set name=REGEXP_REPLACE(name, E'''', '''''', 'g');
(skip the final 'g' if you only want to replace the first ')
or without resorting to regexp:
update customer set name=REPLACE(name, '''', '''''');
I'm trying to insert some data into my table using the copy command :
copy otype_cstore from '/tmp/otype_fdw.csv' delimiter ';' quote '"' csv;
And I have this answer :
ERROR: unterminated CSV quoted field
There is the line in my CSV file where I have the problem :
533696;PoG;-251658240;from id GSW C";
This is the only line a double quote and I can't remove it, so do you have some advice for me ?
Thank you in advance
If you have lines like this in your csv:
533696;PoG;-251658240;from id GSW C";
this actually means/shows the fields are not quoted, which is still perfectly valid csv as long as there are no separators inside the fields.
In this case the parser should be told the fields are not quoted.
So, instead of using quote '"' (which is actually telling the parser the fields are quoted and why you get the error), you should use something like quote 'none', or leave the quote parameter out (I don't know Postgres, so I can't give you the exact option to do this).
Ok, I did a quick lookup of the parameters. It looks like there is not really an option to turn quoting off. The only option left would be to provide a quote character that is never used in the data.
quote E'\b' (backspace) seems to work ok.
Bit late to reply, but I was facing same issue and found that for double quote we need to add one more double quote to escape with along with prefix and suffix double quote to that special characater. for your case it would be
input data is : from id GSW C"
change data to : from id GSW C""""
note there are 4 consecutive double quotes.
first and last double quote is prefix and postfix as per documentation.
middle 2 double quotes is data with one escape double quote.
Hope this helps for readers with similar issue going forward.
So for every double quote in data it needs to be escaped with one escape character (default double quote). This is as per documentation.
I'm using Powershell with ODBC to transfer table data between Sage 50 and MariaDb.
Some data rows in a text column in Sage can contain both single and double quotes that need to be retained when the data is imported into MariaDb.
I'm struggling to get PowerShell to replace for the Values portion of the Insert statement:
I need to replace a single quote ' with backslash single quote \', and also a double quote " with backslash double quote \"
For anyone else searching for this the answer is :
For single quotes
[regex]::replace($text, "'", "\'")
For double quotes
[regex]::replace($DETAILS, "`"", "\'")
I found this URL helpful https://vwiki.co.uk/MySQL_and_PowerShell and as stated you may wish to put this in a function.
/[Path='/'].applicationPool:"ASP.NET v4.0"
This whole line is actually a parameter, so I would like to put single quotes around it. But it doesn't work because it already includes single quotes.
I have tried the ` in front of the single quoates without success.
Any suggestions?
You have to surround with quotes and escape the inner quotes with backtick:
"/[Path='/'].applicationPool:`"ASP.NET v4.0`""
Another way would be to double each nested single quote:
'/[Path=''/''].applicationPool:"ASP.NET v4.0"'