SQL NOT LIKE not functioning properly - mysql

I am unable to figure out why the NOT LIKE operators are not working properly. I have 34 sample_name in the database that start with NT but it only get rid of some, and not other. When I try searching with LIKE ('NT%), it search them all. Below is what I have.
WHERE
full.full_id IN ('272', '273')
OR full.full_id IN ('567', '686')
AND random.sample_name NOT LIKE ('NT%')

You have an extraneous , in the IN list and you are missing the ' at the end of your NOT LIKE statement. The statement should look like the following:
WHERE full.full_id IN ('272', '273', '567', '686')
AND random.sample_name NOT LIKE ('NT%')

You have several errors in the code you have posted. I am going to guess that you intend:
WHERE full.full_id IN (272, 273) AND
random.sample_name NOT LIKE 'NT%'
Notes:
No comma in the IN list.
Terminating single quote on LIKE pattern
Don't use single quotes for constants, if the column is a number (use single quotes only for string and date constants).
No parentheses are needed around the LIKE pattern (although that is not a syntax issue)

Related

MySQL replace multiple character

I need to fetch data from my MySQL database where one column is filled with strings that contains different quotes and apostrophes (‘, ’, ` and the classic one ').
In my search bar, I want to be able to only write the classic one ' and be able to search for that specific field wheter it contains ‘, ’ or `.
For example, I'd like to write "Regie de l'Angleterre" and that it automatically checks "Regie de l‘Angleterre" and so on.
Is that possible ?
How can I do something like this in SQL ? I tried to use REPLACE, but couldn't make it work with multiple
My where clause looks like this at the moment.
SELECT realestate_name FROM realestates WHERE REPLACE(realestate_name, ''','\'') LIKE %xxx%
SELECT realestate_name FROM realestates WHERE column LIKE '____'
Before you do that, change every single ' " ` and quotation mark and space to %, and put them on the ends
So it would look like this:
Regie de l‘Angleterre becomes: %Regie%de%l%Angleterre%
SELECT realestate_name FROM realestates WHERE realestate_name LIKE %Regie%de%l%Angleterre%
This will work & run fine with a not so large database. If you have millions of records, LIKE becomes a nightmare.
You can use multiple replaces in one select the Replace function returns string so you can call another one outside it. Like this
SELECT
realestate_name
FROM realestates
WHERE REPLACE(REPLACE(realestate_name, ''','\''),'’','\'') LIKE %xxx%

Why do all my SQL queries have to be wrapped using the ` symbol?

I have been working on a database for my coursework and have used phpMyAdmin to build it. Now I am working on the queries using the query tool.
When I pick the tables and data I want to query and press "update query" it will generate the query which looks something like this:
SELECT `Customer`.`CustomerName`, `OrderDetails`.`Product`, `OrderDetails`.`QuantityOrdered`
FROM `Customer`
LEFT JOIN `Order` ON `Order`.`Customer` = `Customer`.`CustomerID`
LEFT JOIN `OrderDetails` ON `OrderDetails`.`Order` = `Order`.`OrderID`
This works fine and gives me the results I was expecting. However when I try and write my own query and put something like "SELECT Customer.CustomerName," WITHOUT the ' symbol it won't work and just throws up an error message.
Must I always wrap them using the ' symbol for the query to work?
Forward quotes are used to escape object names in MySQL. You don't have to use them unless you use names that wouldn't be valid identifiers - in this case, the table name order is a reserved word, and must be escaped. All the other tables and columns you're using seem to be OK.
Except for the visual nightmare and ability to create horrendous table names, backticks are entirely unnecessary. You will, however, be required to wrap any variables in single quotes.
As you can see from my example below, using backticks is not a requirement with PHPMYADMIN;
The reason it is not working when you remove the backticks is because you have a column called 'order'. Order is a keyword in SQL and therefore cannot be used as a column name without being wrapped in either quotes or backticks.

MySql regexp escaping apostrophe(’)

I can't find a proper way to escape apostrophe sign(’) in my mysql query. Regexp I have, works fine with online tools for regexp testing.
Problematic example is the string G’Schlössl.
I want to have optional apostrophe sign in the query in front of the s character G(’?)Schlö(’?)ssl for all the different cases which could occur in other strings. I am not sure if the problem is caused by incorrect sign escaping but I have tried many options like ’?, \’?, \’{0,1} which works for the first occurrence but doesn't for the second optional one and cause query to return nothing. Other possibilities like ’’?, [’]?, [\’]?, [\’]{0,1} does not work even for the first one.
select id, name from restaurant where name regexp '.*g\’?(s|ß|ss|sz)chl(o|ö|oe)\’?s.*';
When I remove the last \’? it works:
select id, name from restaurant where name regexp '.*g\’?(s|ß|ss|sz)chl(o|ö|oe)s.*';
When I replace the last \’? with x? it works as well:
select id, name from restaurant where name regexp '.*g\’?(s|ß|ss|sz)chl(o|ö|oe)x?s.*';
Any ideas where the problem is or what else to try?
This thread explains escaping normal single quote only, which seems not to work in my case.
Instead of \’?, try (’)?. I'm thinking that the ? may apply to only the last byte of ’. By using parentheses instead, the ? applies to the entire 3 bytes (hex E28099) of the "RIGHT SINGLE QUOTATION MARK".

Using MySQL LIKE operator for fields encoded in JSON

I've been trying to get a table row with this query:
SELECT * FROM `table` WHERE `field` LIKE "%\u0435\u0442\u043e\u0442%"
Field itself:
Field
--------------------------------------------------------------------
\u0435\u0442\u043e\u0442 \u0442\u0435\u043a\u0441\u0442 \u043d\u0430
Although I can't seem to get it working properly.
I've already tried experimenting with the backslash character:
LIKE "%\\u0435\\u0442\\u043e\\u0442%"
LIKE "%\\\\u0435\\\\u0442\\\\u043e\\\\u0442%"
But none of them seems to work, as well.
I'd appreciate if someone could give a hint as to what I'm doing wrong.
Thanks in advance!
EDIT
Problem solved.
Solution: even after correcting the syntax of the query, it didn't return any results. After making the field BINARY the query started working.
As documented under String Comparison Functions:
Note
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.
Therefore:
SELECT * FROM `table` WHERE `field` LIKE '%\\\\u0435\\\\u0442\\\\u043e\\\\u0442%'
See it on sqlfiddle.
it can be useful for those who use PHP, and it works for me
$where[] = 'organizer_info LIKE(CONCAT("%", :organizer, "%"))';
$bind['organizer'] = str_replace('"', '', quotemeta(json_encode($orgNameString)));

A couple of basic Sql Profiler questions

(Sorry for the longish question, I'll try to be concise.)
I'm running SQL Server Profiler and I'm chasing down some performance issues. I'm relatively new to what the profiler does and I've exported the traces into a table so I can run queries against the data.
One thing I've been running up against is some seemingly odd behavior doing select queries against the TextData field of the table generated by the trace export. It may have to do with the field's data type (ntext, null). I'm selecting for particular values, but getting unexpected results. For example, if I do this:
select * from [TraceAnalyzer].dbo.TraceTable
and I'm interested in values like this:
exec [Sproc_of_interest] #parm1=992
I'd do a query like this:
select * from [TraceAnalyzer].dbo.TraceTable
where TextData like '%exec [Sproc_of_interest] #parm1=%'
but the return result is empty.
Also, if I do a query like:
select * from [TraceAnalyzer].dbo.TraceTable
where TextData like '%exec [Sproc_of_interest]%'
I get unexpected TextData values like exec sp_reset_connection
Would the square brackets in the criteria be messing things up? I've tried omitting them, but that just excludes everything. I'm not aware of escape characters in SQL select queries, but when I copy/paste the value from one of the offending records, the pasted value does not appear to contain anything that would meet the original query's criteria.
Any insights would be greatly appreciated. Thanks.
[Sproc_of_interest] in the pattern syntax is interpreted as matching one character that is in the set S,p,r,o,c,_,o,f,_,i,n,t,e,r,e,s,t.
Three possible ways of solving this are below.
1) Escape [ with square brackets
LIKE '%exec [[]Sproc_of_interest] #parm1=%'
2) Use an escape character
LIKE 'exec \[Sproc_of_interest] #parm1=' ESCAPE '\'
3) Use CHARINDEX instead of escaping anything
WHERE CHARINDEX('exec [Sproc_of_interest] #parm1=' , TextData) > 0