Using REPLACE() in MySQL SELECT - mysql

I apologise in advance for asking what I'm sure will prove to be a very simple question.
I have a MySQL (5.5) database that includes, amongst other things, a field for telephone numbers. I'm trying to create a statement that will search that field, stripping out any spaces. So searching for '0208' will return '020 8', '02 08', '0 208', ' 0208', etc.
And this is in Delphi XE2, in case that makes a difference.
'SELECT * FROM sales_ledger WHERE REPLACE(telephone, " ", "") LIKE "%' + SearchEdit.Text + '%"'
...gives me an error...
Invalid filter in WHERE clause
...and...
'SELECT REPLACE(telephone, " ", "") FROM sales_ledger WHERE REPLACE(telephone, " ", "") LIKE "%' + SearchEdit.Text + '%"'
...gives me...
Invalid field name. General SQL error. Column not found.
...and I do actually need all fields returned anyway.
May I ask for some assistance in correcting the syntax please. If you need more information, don't hesitate to ask. Thanks very much for your time.
EDIT: One potentially critical piece of information I missed out. The table is actually a Sage database that I'm accessing through ODBC. As nothing I try is working that may well be the root problem. Apologies for not saying that earlier.

Your Query Seems Working Fine see the demo
First try this query to you DB Client
SELECT * FROM sales_ledger
WHERE REPLACE(telephone, " ", "") LIKE "%0208%"
EDIT:
if you query is still not working. fellow step by step process.
try to run a simple select query (SELECT * FROM sales_ledger)
if the first query run try adding simple where condition.
so step by step you can make your query similar to original one and find where is the actual error from.

Try this ::
SELECT
REPLACE(telephone,' ', '') as replacedColumn
FROM sales_ledger
WHERE REPLACE(telephone,' ', '') LIKE '%"+ SearchEdit.Text +"%'"
OR
Select temp1.*
from
(
SELECT
REPLACE(telephone,' ', '') as replacedColumn
FROM sales_ledger
) as temp1
WHERE temp1.replacedColumn LIKE '%"+SearchEdit.Text+"%'"

Related

Why rowcount return -1 when row does exist?

I desesperatly try to check if a row exist with python in my mysql database.
I spent all my afternoon to try to find out!!!
I am impressed how such a useful function doesn't work properly after decades of existance! It is unbelievable. I guess expert will laugh about me :-). I am a newbie. So I aplogie for this simple question.
I've tried it all. I tried the "SELECT exist", I tried "to count the rows" and so many other stuff..... The countrows is the most common but it return -1 when I have existing rows in my database. I don't understand!
I tested from my pycharm and from phpmyadmin.
I attached screenshot.
sql = "select * from websites where website = '" + item["website"] + "' limit 1"
self.curr.execute(sql, multi=True)
if self.curr.rowcount == 1:
print("Website found")
Does anyone can tell me how can I check if a row exist or not in a mysql table?
Thanks for your help.
Ok. I finally found the answer by testing hundreds of things. I had to remove the "multi=True". And I guess I had to add "self.cur.fetchall()" like suggested #Raymond Nijland
Here is the code which finally works:
sql = "select * from websites where website = '" + item["website"] + "'"
self.curr.execute(sql)
self.curr.fetchall()
if self.curr.rowcount == 1:
print("Website found")
Thank you all for trying to help me.

SQL Query WHERE clause is a parameter

Can you please help me solve the problem I am having with my query. I am very new in Database programming and I thoroughly search the web for the help but none solve my issue. I am fed up and wondering if this is even possible.
What I want to accomplish is to execute a SELECT query where the column to search is to be supplied on runtime. Like this one:
SELECT *
FROM myTable
WHERE #columnToSeach = #_ColumnName
Advice is really appreciated as my brain is practically bleeding on this one.
A very quick solution would be:
execute ('SELECT * FROM myTable WHERE ' + #columnToSeach + '=' + #_ColumnName)

How to query using LIKE clause in mysql using vb.net comparing to a varied string input?

I've search and tried a lot of procedure but did not work.
CREATE PROCEDURE did not work, SET #variable is not acceptable and a couple more codes. This is my last "query" code that still didn't work.
qry = "select * from employeefile where empfname LIKE '%''" + input + "''%'"
empfname is a name of an employee from table employeefile that possibly consists of three words. The input could be the first word, second word, third word or the entire words.
when i tried to input any word within the name, the program will still prompt, "no records found." when i tried to change the query into
qry = "select * from employeefile where empfname LIKE '%existingname%'"
and my input is "existingname", the program runs just as i want it to.
This code is one of those that i seached but still didn't work.
T-SQL and the WHERE LIKE %Parameter% clause
How to use like clause in MySQL 5.0 Statement
T-SQL and the WHERE LIKE %Parameter% clause
The problem here is when i use a variable... i probably get the wrong way of using it into query. Please help me. I am new here by the way.
If I'm understanding your question correctly:
Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE '%' + #empfname + '%'", connection)
command.Parameters.AddWithValue("#empfname", input)
or:
Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE #empfname", connection)
command.Parameters.AddWithValue("#empfname", "%" & input & "%")
You have to concatenate the wildcards with the input text, either in your SQL code or your VB code.
This is the correct syntax: LIKE '%blah%', but this code does not put the quotes outside the %s: LIKE '%''" + input + "''%'".
i got the answer. it turns out that i just overdid the single quote. it must be written this way:
qry = "select * from employeefile where empfname LIKE '%" + input + "%'"

how to get Even/Odd id numbers from a database table in codeigniter

I am new in codeigniter and just stacked in a query to solve a report for an emergency project. Please help me Codeigniter's Experts.
I have a large database table and wants to show only Odd/Even Data rows from that table which will filtered by a table Field named is "sale_id". I tried it in PHPMyadmin in raw coding and it's worked for me. But can not apply in Codeigniter.
SELECT * FROM ospos_pak_sub_cat WHERE id %2 =0;
Worked for me in raw PHP Coding. How can I use it in Codeigniter. I used a Where Condition already on that query and now want to add the new query.
Existing Where condition is given below, which is working fine.
$this->db->where('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
It is working and I tried the code below to get the solution which is not working and getting error.
$this->db->where('sale_id %2'=> 0);
Getting error with this line. says--
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)
Please help me to get the solutions. Thanks in Advance.
In your Model, just write the query like that:
$this->db->select('*');
$this->db->from('ospos_pak_sub_cat');
$this->db->where('sale_id %2=', 0);
$query_result = $this->db->get();
$result = $query_result->result();
You missed the '=' in your code. Hope, it will work.
in SQL the % character is a wildcard rather than a modulo which would explain your error. you can use the MOD function instead http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_mod
so the resulting code would be :
$this->db->where('MOD(sale_id ,2) => 0');
I am not sure but i think it will work try this line
$this->db->where('sale_id %2',0);
As you mentioned, you can add the new condition in existing where condition as like below
$this->db->where('(sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].')" and ((sale_id % 2) = 0)');

Mysql use parsed column from SELECT in WHERE condition

How can I do something like this:
SELECT DID, DNumbers, CONCAT(SUBSTRING_INDEX(DNumbers, " - ", 1), ", ") AS DNumbers_Parsed
FROM Test
WHERE DNumbers_Parsed LIKE '%, 1,%' AND DNumbers_Parsed LIKE '%, 5,%'
This gives an error saying column DNumbers_Parsed not found. I know I can just use the same parsing thing for each condition but is it possible to only do it once and then use it in conditions?
Generally speaking, you can't use derived fields in a where clause, as the value of the derived field MAY not be available yet at the time the where filter is being applied (e.g. aggregate function results).
Try using a HAVING instead:
SELECT ...
FROM ...
HAVING DNumbers_Parsed LIKE ...
HAVING is applied just before the results are sent to the client, after all aggregate operations/field value derivations have been completed.
and note that your code above has a typo: AN DNumbers_....
well the having clause posted by Marc B is a good solution.. even though having is generally slow... another way you could try it is in a sub query so that way the data is already returned from the server.
SELECT *
FROM(
SELECT DID, DNumbers, CONCAT(SUBSTRING_INDEX(DNumbers, " - ", 1), ", ") AS DNumbers_Parsed
FROM Test
)t
WHERE t.DNumbers_Parsed LIKE '%, 1,%' AN t.DNumbers_Parsed LIKE '%, 5,%'