MySQL backslash apostrophe - mysql

I'm having a MySQL issue.
I'm trying to select all rows in a table that start with a backslash and an apostrophe:
SELECT * FROM table WHERE name like '\\\'%'
But this is not working. An example of what I'm trying to select: \'S-GRAVENDEEL
How do I do this?
Thanks
p.s.
Yes, this was the result of a faulty import, I know, but now I need to fix it :-)

You need more backslashes:
select * from table where name like '\\\\\'%'
You need one of them to get a single quote into the pattern. Then you need four more to get a single literal backslash down to the like. Or you could escape the single quote by doubling it:
select * from table where name like '\\\\''%'

So I've got a solution.
Basically what I want to do is fix the entries, so I'll show you what the replace looks like:
SELECT *, REPLACE(naam, '\\''', '''') naamnew FROM school_plaats WHERE naam like '%\\''%'
Apparently, I need to escape the apostrophe with an apostrophe and the backslash with a backslash.

Related

mysql search where column like ''

Quick questions, I bet the answer is so simple and I am just being blind.
I want to select from the database all the names that only start with "test_1_1_".
I would guess that I do this with;
SELECT * FROM my_table WHERE names LIKE "test_1_1_%";
This doesn't seem to work.
The results keep showing up as
test_1_1_1
test_1_1_2
test_1_11_1
test_1_11_2
test_1_12_1
test_1_12_2
How can I select with MySQL only the results that start with "test_1_1_"?
Thank you in advance.
Wesley
Underscore has a special meaning in LIKE and needs to be escaped:
SELECT * FROM my_table WHERE names LIKE 'test\_1\_1\_%';
You could also use REGEXP here, and avoid the escaping problem:
SELECT * FROM my_table WHERE names REGEXP '^test_1_1_';
When you use _ inside LIKE, it will mean replace that space with any character.
For example, searching for something like field LIKE 'a_' will result in any field with 2 characters starting with "a".
If you really want to search for the underscore characters, you need to escape the value with \ and your query will look like this: LIKE 'test\_1\_1\_%';
The underscore is a single character wildcard when use with LIKE. So to specifically locate an underscore it needs to be "escaped" as follows:
select
*
from mytable
where names like 'test\_1\_1\_%' escape '\'
To test for literal instances of a wildcard character, precede it by
the escape character. If you do not specify the ESCAPE character, \ is
assumed.
https://dev.mysql.com/doc/refman/8.0/en/string-comparison-functions.html
Can do this:
SELECT * FROM my_table WHERE names LIKE "test\_1\_\1\_%";

SQL - How to use wildcard in LIKE as a normal character

if I have a column with some values that starts with "%" like this:
[ID]-----[VALUES]
1--------Amount
2--------Percentage
3--------%Amount
4--------%Percentage
how can I have only these two rows with a "select" query?:
[ID]-----[VALUES]
3--------%Amount
4--------%Percentage
I tried these queries but them don't work:
select * from TABLE where VALUES like '[%]%'
select * from TABLE where VALUES like '\%%'
I know that in Java, C and other languages, the backspace \ let you use a jolly character as a normal one like:
var s = "I called him and he sad: \"Hi, there!\"";
There is a similar character/function that do it in SQL?
All answers will be appreciated, thank you for reading the question!
Your query
select * from TABLE where VALUES like '\%%'
should work. The reason it doesn't is because you may have NO_BACKSLASH_ESCAPES enabled which would treat \ as a literal character.
A way to avoid it is using LIKE BINARY
select * from TABLE where VALUES like binary '%'
or with an escape character (can be any character you choose) specification.
select * from TABLE where VALUES like '~%%' escape '~'
try this :
select * from TABLE where VALUES like '%[%]%'
There is an ESCAPE option on LIKE:
select *
from TABLE
where VALUES like '$%%' escape '$';
Anything following the escape character is treated as a regular character. However, the default is backslash (see here), so the version with backslash should do what you want.
Of course, you could also use a regular expression (although that has no hope of using an index).
Note: escape is part of the answer standard so it should work in any database.
You're right that you'll need an escape character for this. In SQL you have to define the escape character.
SELECT * FROM TABLE where VALUES like ESCAPE '!';
I'm pretty sure you can use whatever character you want.
Here's a link to a microsoft explanation that goes into more detail.
Microsoft explanation
MySQL Explanation

Search of String value having special character in MySQL

I have one table emp in MySQL database having column as name. In that name column, the value is 'abc\xyz'. I want to search this value. I have tried using following query:
select * from emp where name like 'abc\xyz';
Also i have tried
select * from emp where name like 'abc\xyz' escape '\\';
But i did not found any output. Could you please help me in finding such strings? Such strings can have special character at any location.
Thanks in advance.
You may try like this:
select * from emp
where empname like '%abc\\\\xyz%'
SQL Fiddle Demo
From the docs:
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
SELECT REPLACE(text,'\\','') FROM tbl
You can use REPLACE to remove some special chars :)

phpmyadmin sql apostrophe not working [duplicate]

This question already has answers here:
character for single quote
(1 answer)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
hey guys was hoping you could help me out,
Not sure if I always had this problem or if its new, in phpmyadmin in the sql tab, the apostrophe it recognizes is different from what i type, for example,
when i type, it is
SELECT * FROM 'table'
this gives me an error, so instead I have to like copy/paste the inverted commas of some prebuilt query so that it looks like
SELECT * FROM `table`
see how the apostrophes are different? any way I can fix this?
also, i have seen many queries on the web, and i think even queries i call from php dont require table names to have apostrophes. But when write it in phpmyadmin, I can do queries without table names having apostrophes?
thanks in advance.
In MYSQL, table is a reserved keyword. If you want to use reserved keywords in mysql in query, you have to enclose them in backtick(`).
As table is reserved keyword you query should be
SELECT * FROM `table`
Regarding single quote ('), in mysql, it represents string value.
SELECT *, 'table' FROM `table`;
Demo
You should only need to quote table names when they conflict with a reserved word.
Also:
` = Grave accent, or (because someone needed to invent a word) backtick
' = Apostrophe, or straight single quote
You dont need apostrophe on table name.
You should use ` in cases that your table/field name is a reserve word eg:
SELECT `distinct`, myfields FROM mytable
note that distinct is an sql command so you need to put the `.
SELECT * FROM `table`
table here should be inside `.
There are two different characters, the backtick and the single quote. Table and column names can be surrounded by the backtick, strings can be surrounded by quotes. There is nothign to fix :D

MySQL: how to use comma, single quote, and double quote as columns?

I'm trying to pivot a table so I can output the data as a CSV. I need to do something like this:
SELECT .... t1.`column_one`, t1.`column_two`, ...
Problem is that some of the columns are expected to contain commas, single quotes, and double quotes.
Is there a way to make something like this work:
SELECT .... t1.`foo's, "bar"`, ...
The above doesn't work. Suggestions?
I've tested and can confirm that the following definitely works:
SELECT `t1`.`foo's, "bar"` FROM `t1`;
The only thing I could suggest is to place the table name between ` (backtick) characters.
According to the MySQL documentation you should be fine as long as the column name is ASCII, doesn't contain an ASCII NUL (0x00) and is under 64 characters long in total. What do you mean by "doesn't work"? Does it give an error message?