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
Related
I am trying to remove the spaces, whitelines and tabs from the user his input when he searches in the sql database.
So basicly he searches 'Test' (two spaces behind the string) it gives zero results while 'Test' is a correct result in the database. How do I make php / sql use only the word 'Test' as keyword in the sql statement so the result is generated even when the user post spaces before or behind the string. We use a lot of copy and paste when searching so spaces and stuff are in the paste a lot of the time.
I am aware of the questions that are allready here. And I tried all I could implent (as you can see in the overkill tryout..) but none seem to do the trick..
Please help, what am I doing wrong here?
$searchresult = trim(filter_var(isset($_POST['usearch10'])?$_POST['usearch10']:'' , FILTER_SANITIZE_STRING));
$save = trim(filter_var("%{$_POST['usearch10']}%", FILTER_SANITIZE_STRING));
$safedef = trim($save);
$stmt2 = $mysqli->prepare("SELECT * FROM table WHERE `colomn` LIKE trim(?)");
$stmt2->bind_param("s",$safedef);
$stmt2->execute();
$result = $stmt2->get_result();
while ($obj = $result->fetch_assoc()) {
You need to do two things. One is remove the spaces, and trim is enough for that. Unless you're copy-pasting from Microsoft Word, where you could get hidden whitespaces, nonbreaking spaces, and possibly snakes. To get rid of those you might need to use a regular expression (requiring UTF8/mbstring support),
$search = preg_replace('#^\\s*(.*)\\s*$#', '\\1', $search);
I am not sure, but maybe if you copy and paste from HTML you might even encounter a " " instead of a space, so you might want to also use html_entity_decode (after verifying you need it - it can be quite the source of other troubles).
Then you need to specify an inclusion; do that after the filtering, just in case, like this
$search = trim($search);
$search = "%{$search}%"; // search term anywhere
WHERE `column` LIKE ? # You wrote 'colomn' but I assume it was a typo
This simply has to work, but you could also do it this way:
...WHERE `column` LIKE CONCAT('%', ?, '%');
to be sure that MySQL understands what you're trying to tell it.
Try also printing the search term and the query and running it by hand to see what happens. And always check that the query did run successfully. To be even safer, dump the json_encode($search). So, if you forgot a semispace, you'll see not the expected "Test" but something like, if memory serves, "Test\u00a0".
If the absolute worst comes to the worst, activate the session-level "general_log" in MySQL and inspect the actual query MySQL gets sent in the MySQL log_file.
I have never used trim in SQL, but if that is changing something or not, your problem could be that you are not searching like LIKE ”%STRING_TO_SEARCH%". % at the beginning will tell to match anything before it, and the same character at the end will tell to also match anything after your desired string.
I have done my research on the subject and I thought about using replace, but the trouble is if I replaced with nothing, then I will end up with lots of spaces in my database field which will get very messy. So I have come to you guys for help.
I have a field in my database called EQUIPMENT. This is an example of what is in one of the fields
15<>5<>6<>3<>2<>1<>14<>13<>12
Each number is representing the ID of a piece of equipment. How would I go about deleting for example <>6
As I said, I looked at using replace but replacing it with ' ' would leave spaces in the field.
Replace with '' (no space) should not leave a space. You could also use trim, if you don't want any whitespaces.
I m not sure if this is what you want, but before inserting value just use:
echo str_replace("<>", "", "15<>5<>6<>3<>2<>1<>14<>13<>12");
Output: 1556321141312
To delete <>6 use echo str_replace("<>6", "", "15<>5<>6<>3<>2<>1<>14<>13<>12");
Outcome: 15<>5<>3<>2<>1<>14<>13<>12
If you want to do this on mysql database then this could help you: https://stackoverflow.com/a/14586441/5459942
Instead of replacing a "<>number" by an empty string, you can replace a "<>number<>" by "<>".
This is to avoid that other numbers (that contain that number) are also replaced partially.
And to avoid a wrong result when the number is at the start or the end of the string, a '<>' is concatinated to both ends.
Then those '<>' at both ends can be trimmed after the replace.
Example:
select
EQUIPMENT,
TRIM(BOTH '<>' FROM REPLACE(CONCAT('<>',EQUIPMENT,'<>'),CONCAT('<>',6,'<>'),'<>')) as Equipment_without_6
from
(
select '15<>5<>6<>3<>2<>1<>14<>13<>12' as EQUIPMENT
union all select '6<>16<>6<>60<>6'
) q
Returns:
EQUIPMENT Equipment_without_6
------------------------------ ---------------------------
15<>5<>6<>3<>2<>1<>14<>13<>12 15<>5<>3<>2<>1<>14<>13<>12
6<>16<>6<>60<>6 16<>60
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.
I have a database class that is written in PHP and it should take care of some things I don't want to care about. One of these features is handling the decryption of columns that are encoded with the AES function of MySQL.
This works perfect in normal cases (which in my opinion means that there is no alias in the query string "AS bla_bla"). Lets say that someone writes a query string that contains an alias, which contains the name of a column the script should decrypt, the query dies, because my regex wraps not only the column, but the alias as well. That is not how its supposed to be.
This is the regex I've written:
preg_replace("/(((\`|)\w+(\`|)\.|)[encrypted|column|list])/i", "AES_DECRYPT(${0},'the hash')"
The part with the grave accents is there because sometimes the query does contain the table name which is either inside of grave accents or not.
An example input:
SELECT encrypted, something AS 'a_column' FROM a_table;
An example output:
SELECT AES_DECRYPT(encrypted, 'the hash'), something AS 'a_AES_DECRYPT(column, 'the hash')' FROM a_table;
As you can see, this is not going to work, so my idea was to search only for words, that are not right after the word 'as' until a special character or a white space appears. Of course i tried it hours to work, but I don't get the correct syntax.
Is it possible to solve this with pure regex and if yes how would it look like?
This should get you started:
$quoted_name = '(\w+|`\w+`|"\w+"|\'\w+\')';
preg_match("/^SELECT ((, )?$quoted_name( AS $quoted_name)?)* FROM $quoted_name;$/", "SELECT encrypted, something AS 'a_column' FROM a_table;", $m);
var_dump($m);
The replacement parts should be easy to spot an write after you study the var_dump.
In a database, I have some text stored in a field call Description, the value of the string saved in my database is Me\You "R'S'" % and thats how it appears when querying the database command line.
Now, on a web page i have a function which searches this field as such:
WHERE Description LIKE '%$searchstring%'
So when $searchstring has been cleaned, if i was searching for Me\You, the backslash gets escape and my query reads:
WHERE Description LIKE '%Me\\You%'
However it doesn't return anything.
Strange part of this, is that when i search Me\\You or Me\\\You (So two or three backslashes, but no less or no more) it will return the result i expect with one backslash.
When querying for the result command-line, it does not return a result for:
WHERE Description LIKE '%Me\You%'
or when i use two or three backslashes.
However it will return the result if i use 4 - 7 backslashes, for example:
WHERE Description LIKE '%Me\\\\\\\You%'
will return the string which is Me\You "R'S'" %
Anyone have a reason to this happening? Thanks
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.
Source: http://dev.mysql.com/doc/refman/5.1/en/string-comparison-functions.html#operator_like
Read this Need to select only data that contains backslashes in MySQL to see how to use double backslash escaping. You could also run MySQL in NO_BACKSLASH_ESCAPES mode (http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_no_backslash_escapes)
Although an old post, you can bypass this limitation using replace function to change backslash to another character: something like this in the WHERE clause. EXAMPLE:
WHERE replace('your field here', '\', '-') like "You-Me%"