To find and replace escaped quotes in MySQL table - mysql

My database has content that has been previously escaped, resulting in string such as
This value is \"invalid\".
I want to get rid of escape character \ but I'm having a hard time to find these rows. My first attempt
select value from content where value like '%\\"%';
fails to separate \" from ", and returns valid rows such as
This value is "valid".
So how can I query for the string \", preferably in a way than can be used in an update clause to remove the slash?
EDIT: SQL Fiddle here http://sqlfiddle.com/#!9/fc3d3/6
Notice that the query at line 3 returns both rows.

I've checked your sqlfiddle.
This gets the invalid rows:
SELECT * from myTable where content<>REPLACE(content,'\\\"','\"')
If this works, then you can simply update your content column to REPLACE(content,'\\\"','\"').

Related

Maria DB - Prevent removal of trailing white space in where clause

MariaDB version 10.3.22 and 10.3.23
I have a table with column name entries like these. The second entry contains trailing spaces. Datatype of the column is Varchar(50)
"John"
"John "
I want the below query to return only the first entry, but it returns both entries. Seems like internally, it's doing the trim while comparing. Any way to perform the exact match
Select name from <table> where name="John";
Similarly, the below query should not return any rows as I'm trying to match with extra space, but it outputs two rows. Some behind the scene trimming is happening. Need to either disable it or anyway to do the exact match
Select name from <table> where name="John ";
Use the BINARY option to compare exactly rather than as strings.
Select name from <table> where BINARY name="John";

Select from a field containing spaces using MySQL

I'm attempting to query on a field/column/table in a MySQL DB where the field type is varchar, but some values contains spaces. In my query, I tried to put the exact string to match on in single quotes in a where clause. However, the only rows that are returned are the strings that do not contain spaces.
Here are the values stored in the table/column:
Here is the query and the result that is only returning fields without spaces:
I expected to find a row for "New Business", a row for "Monetary Endorsement", etc. Any idea on how I can modify my query to return the desired fields? Thanks for your help in advance!
Maybe the other values have leading or trailing spaces. You can either use one of the suggestion below:
1.) Use TRIM()
WHERE TRIM(PTD_TRANS_TYPE) = 'NEW BUSINESS'
2.) Use LIKE
WHERE PTD_TRANS_TYPE LIKE '%NEW BUSINESS%'
Here's a Demo.

MYSQL regular expression matching any word in between square brackets

I have to find all the values of a specific column in a table where the column values match like [name]
I should not get the values that are like [a]+[b] or [a]>[b] or [a]%[b]=[c]
So I basically do not need column values that have special characters in them except the square brackets and under score
example: [test_123] should be returned.
I tried
select * from table_name where column_name REGEXP '^[[][^+-></%]';
This is just trying to see if there is any special character immediately after [ but how to see if there is any special character in the whole column value and should we give backslashes to define special characters in MySQL?
I tried in https://regexr.com/ and I have got my required Regex to be
(\[)\w+[^\+\=\/\*\%\^\!](\])
but I could not do the same in MySQL
*** UPDATED **
(\[[^\_]+])+
That seems to work for what you're looking for.
Also your query is wrong, I believe it is supposed to look like:
SELECT * FROM mytable WHERE REGEXP_LIKE(mycolumn, 'regexp', 'i');

Replacing single quote characters in a column in Amazon Redshift

I have some data in a table in the following format in Amazon Redshift:
Column1 Column2
'a' 'b'
I'd like to remove the single quote characters from this table and get the data as follows:
Column1 Column2
a b
I would have thought the Replace function would do the trick, so I wrote the following query:
select replace(column1,''',''),
replace(column2,''','')
from table
But this doesn't work and gives me Amazon](500310) Invalid operation: unterminated quoted string at or near "''',''). I tried to escape the single quote character by \ but even that didn't work.
I also tried using the following query:
select replace(column1,"'",''),
replace(column2,"'",'')
from table
But it gave me the error [Amazon](500310) Invalid operation: column "'" does not exist in <tablename>
So how do I remove these single characters from my data?
Any help would be much appreciated.
TIA.
select replace(column1,chr(39),''),
replace(column2,chr(39),'')
from table
The CHR function returns the character that matches the ASCII code point value specified by of the input parameter. If I’ve made a bad assumption please comment and I’ll refocus my answer.
With MySQL you have two ways of quoting, so you need to switch:
REPLACE(column1, "'", "")
You can't use the same character for both delimiting and content without escaping:
REPLACE(column1, '\'', '')

How to do a correct query in mysql with in the string \40

I have this string: alexandre.aba\40gmail.com#gtalk.ofelia.dcc.fc.up.pt stored as JabberID in USER table in my database.
The problem is when i execute this query:
SELECT * FROM `USER` WHERE JabberID='alexandre.aba\40gmail.com#gtalk.ofelia.dcc.fc.up.pt'
It returns:
MySQL returned an empty result set (i.e. zero rows).
I think it's the \40 that is causing the problem but i don't know how to fix it.
You should think about using prepared statements instead since it's safer but to correct the current string look at the link http://dev.mysql.com/doc/refman/5.1/en/string-literals.html for a list of special characters.
I think the \ should be replaced with \\