Unicode characters can be encoded in different ways, canonical equivalence and compatibility. We can normalize them to get the same character as it is important for exact string match. My sample code base
$search = Normalizer::normalize($request->get('search'), Normalizer::FORM_C);
I am normalizing the request search keyword in NFC (Canonical Decomposition followed by Canonical Composition). And then I used that normalized string to search in database.
$products = Product::where('title', "LIKE", "%" . $search . "%")->get();
this line generates a sql query
select * from `products` where `title` LIKE %রায়হান% and `products`.`deleted_at` is null
Here রায়হান is my search string. I get two different encoding for the character য়. One is for য় itself that is %E0%A7%9F and one is %E0%A6%AF%E0%A6%BC that is য + ়
My database values may have both the encoding as I didn't normalize them before saving. So when searching I get the values only those matches the normalized search key. Even if I don't normalize the search key, getting only matching encoded values.
Now I know I can normalize the string before saving and then search with the normalized string. But I already have some values that is not normalized. So is there any way to normalize the string when querying the database? Something like
select * from `products` where NormalizedNFC(`title`) LIKE %my_normalized_value_in_php% and `products`.`deleted_at` is null
Related
I'm trying to do a search in the database with special characters, specifically string with apostrophe.
For example, I want to search for the string: "Sandy's dog", but I just entered "sandys dog" leaving out the apostrophe. Even though "Sandy's dog" exists in the database, it doesn't seem to show it in the results.
Here's my query:
SELECT * FROM `Table` WHERE `Title` LIKE '%sandys dog%'
I have searched everywhere and I can't seem to find a solution that works.
EDIT
Limitations: the string is user generated
Notes:
- If a user searches for sandy's dog with the apostrophe, it works fine as expected.
- Ultimately I would like to get all possible results, if the table contains both strings with and without apostrophe.
In SQL server, you can use REPLACE:
SELECT *
FROM Table
WHERE REPLACE(Title, '''', '') LIKE '%sandys dog%'
The double-apostrophe inside the string is an escape character, so it finds any apostrophes in the string and replaces them with blank strings.
Please try using escape sequences.
http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
Something like, SELECT * FROM Table WHERE Title LIKE '%sandy\'s dog%'
How about this?
SELECT * FROM Table WHERE Title LIKE '%sandy''s dog%'
or
SELECT * FROM Table WHERE Title LIKE '%sandy_s dog%'
The underscore is a "single character" wildcard.
Our accounting application is using Pervasive SQL 10. I need to fetch data of products from it. Problem is that the "name" column has fixed length of 12 and the application is filling the rest with spaces.
So every time I use my PHP script to fetch data, I need to fill the rest of the name with spaces to match it in WHERE clause.
Example data in the column:
65LD11
42BRD03
65LD112
(space)65LD12
165LD12
I have been using: SELECT * FROM products WHERE name LIKE '65LD12%';. Which is not perfect, but the biggest problem is with the name with space as first character, because I can't use _ or % as it would match both 65LD12 and 165LD12 name.
There can be any number of spaces at the beginning or at the end. In MySQL I would use REGEXP_LIKE to match only the spaces, but here in Pervasive I am kind of lost. Is there some way how to do this?
I don't know about Pervasive, but in Standard SQL you can do a simple
WHERE TRIM(name) = '65LD12'
Of course it would be better to clean the data and remove unnecessary leading spaces, TRIM will prevent the usage on an index. And then name = '65LD12' should return the correct data regardless of trailing blanks (again, I don't know if Pervasive implements that correctly)
edit based on comments:
There's no TRIM in Pervasive, but LTRIM:
WHERE LTRIM(name) = '65LD12'
If this is still not returning the correct rows (i.e. Pervasive implemented string comparison in a wrong way) you have to add RTRIM, too:
WHERE RTRIM(LTRIM(name)) = '65LD12'
Try this:
SELECT * FROM products WHERE REPLACE(name,' ','') LIKE '65LD12%';
You can use the Replace function. http://help.pervasive.com/display/DI1025/StrReplace+Function
In our table contains column named as description.
The description column contains following values.
test,%test,test%.
If I give %test as input of search string , then it give "test","%test","test%" as result. But I want only "%test" as result.
% is a special character and needs to be escaped (http://dev.mysql.com/doc/refman/5.0/en/string-literals.html)
Try "\%test"
Snippet:
select description from tablename where decription like '\%test'
I have a MySQL database with a column containing part numbers. Some of the part numbers contain spaces:
3864205010 J
When I query the database or search for the part in phpMyAdmin no results are returned.
Yet when I delete the 2 spaces and then type them again, the search returns a result.
This query does not return a result:
SELECT *
FROM `parts`
WHERE `part_no` LIKE '3864205010 K'
This query returns the result:
SELECT *
FROM `parts`
WHERE `part_no` LIKE '3864205010 K'
They look the same but in the second query I have deleted the 2 spaces before "K" and typed the spaces again.
If you can use wildcard instead of spaces:
SELECT *
FROM `parts`
WHERE `part_no` LIKE '3864205010%K'
This is probably not a space but a HTAB (ascii code 9) or even a line feed/carriage return (10 and 13). Copy paste in a good text editor, you'll see what it really is.
Now, regarding to your wonder about why it doesn't work even if it does look like a space, this is because every single character we see is interpreted by the engine (notepad, phpmyadmin, firefox... any software with text rendering)
What actually happens is that when the engine finds an ascii code, it transforms it into a visible character. The CHAR(9) for example is often transformed into a 'big space' usually equal to 2 or 4 spaces. But phpmyadmin might just decide to not do it that way.
Other example is the line feed (CHAR(10)). In a text editor it would be the signal that the line ends, and (under unix systems mostly) a new line has to start. But you can copy this line feed into a database field, you're just not sure about how it is going to render.
Because they want you to see everything in the cell they may choose to render it as a space... but that's NOT a space if you look at the ascii code of it (and here there's no trick, all rendering engines will tell you the right ascii code).
This is important to always treat characters with their ascii codes.
there's an answer above that suggests using a wildcard instead of the spaces. That might match, or just might not. Let's say your string is '386420K5010', so it is not the one you're looking for, still the LIKE '3864205010%K' pattern would return it. The best is probably to use a regular expression or at least identify the fixed pattern of these strings.
yes as updated question if you wish to remove more space between which contents might be 3 or 4 space below query will use full to you
SELECT REPLACE( REPLACE( part_no, " ", " " ), " ", " " ) from parts.
let me know if it is work for you ?
SELECT *
FROM `parts`
WHERE REPLACE(REPLACE(`part_no`, CHAR(9), ''),' ','') LIKE REPLACE(REPLACE('3864205010 K', CHAR(9), ''),' ','')
This will probably work if part_no and/or search string has tabs and/or spaces.
So i am trying to do a LIKE query and get some results but the text that i pass has some special characters that break the query.
if we assume that the text is something like this:
var test `select` `query`="$newval + "dsadsa$ ? "$test ?
and i also have exactly the same text inside a column as VARCHAR
and then executing the query
SELECT * FROM table WHERE column LIKE '%$text%'
says that there is no rows to return.
EDIT: when i post the data inside the database i simply use mysql real escape string and when i show the text where i click to search i put htmlentities on the text
then i substr it from 0 to 50 and do the search query
You can use mysql_real_escape_string() which will escape any special characters in your string.
Try to avoid writing variables directly into string, it may cause problems (+ it's really not nice):
mysql_query("SELECT * FROM table WHERE column LIKE '%" . $text . "%'");
Of course make sure that the $text variable is really correct (echo $text), characters escaping may cause problems too and of course there can be many other things causing problems (this depends on architecture of your application - where you work with $text).