Wildcard + SQL Parameter in Procedure - mysql

I created a procedure in order to search for a product, but I got a problem, as I would like to use wildcards with a param (nam) , but I don't get them to work.
SELECT * FROM PRODUCTS WHERE NAME LIKE NAM + '%'
SELECT * FROM PRODUCTS WHERE NAME LIKE '%'+ NAM + '%'
...
Thank you so much.

Related

how does ' ' i.e single quote space single quote work in sql?

I observed that this query returns all values from the database.
SELECT * FROM projround3.add_user where user_email like '%' '%';
Why is that?
This query:
SELECT *
FROM projround3.add_user
WHERE user_email like '%' '%';
Would not be valid in most databases. Some interfaces concatenate adjacent strings, so this is interpreted as:
SELECT *
FROM projround3.add_user
WHERE user_email like '%%';
The two wildcards are the same as one. This will match every non-NULL value.
If you want to find emails with a space, then correct logic is:
SELECT *
FROM projround3.add_user
WHERE user_email like '% %';

MySQL CAST and LIKE in WHERE clause

I want to write following query in MySQL and keep getting errors. I have no idea what's wrong, everything seems fine by me.
Query:
SELECT
*
FROM
client
WHERE
id IN (
SELECT
client_id
FROM
url
WHERE
',1,2,' LIKE '%,' + CAST(url_type_id AS CHAR(50)) + ',%'
)
It must have got something to do with the "'%,' + " part, but I honestly don't get it
You have to use CONCAT() to join 2 strings not "+", here is the query which you looking for
SELECT *
FROM client
WHERE id IN ( SELECT client_id
FROM url
WHERE ',1,2,' LIKE CONCAT('%,', CAST(url_type_id AS CHAR(50)), ',%')
)

search database id field with string manipulated id

We will get id from user in this form:
"A-123456-14"
and want to search in database with simple id in this form :
123456
I have tried
select * from orders where id = '%' + searchId + '%';
here id = 123456 in database and searchId = 'A-123456-14' which get from user.
but its not working as its not properly
(A : is prefix, - is delimiter and 14 is postFix)
Please help me to solve this problem.
you can use:
SELECT * FROM orders WHERE WHERE 'A-123456-14' REGEXP '[:punct:]' + id + '[:punct:]';
in above you can replace 'A-123456-14' with user input searchId
i have tried its work fine
SELECT
*
FROM
orders
WHERE
id = SUBSTRING_INDEX(
SUBSTRING_INDEX(searchId, '-', 2),
'-',- 1)
EDIT:
If you want to make everything dynamic then you can try this (You should post delimiter as a parameter also, if it is dynamic).
SELECT
*
FROM
orders
WHERE
id = SUBSTRING_INDEX(
SUBSTRING_INDEX(searchId, delimiter, 2),
delimiter, - 1)
Try this:
SELECT * FROM orders WHERE searchId LIKE CONCAT('%-', Id, '-%')
I would definitely remove the bits of the string you don't need using your programming language of choice (e.g. Java or PHP or whatever you are using), not in SQL. Then just use select * from orders where id = ? and set the parameter to be "%"+id+"%" in your programming language.
Programming languages are for programming, logic, parsing etc., SQL is for defining what you'd like to get from your data
Don't forget to use parameters like ? otherwise you could open yourself up to SQL injection.

MYSQL select where column like column1%column2

I have a table with 3 fields, name, firstname and lastname
I want to see how many rows in the table have name of the form firstname%lastname
I tried to do
select * from family_watchdog_offender where name like firstname%lastname\G
but that returned a syntax error regarding the %lastname portion of the query. Is there some syntax that will allow me to run a query such as this?
SELECT * FROM family_watchdog_offender WHERE name LIKE CONCAT(firstname, '%', lastname);
Try concat-ing the %:
select * from family_watchdog_offender where name like CONCAT(firstname, '%', lastname)
I don't think you can do that...This may be what you need to do
SELECT *
FROM family_watchdog_offender
WHERE name LIKE CONCAT(firstname, '%')
AND name LIKE CONCAT('%', lastname);

Compare value with concatenated fields in where clause

Say I want to search for a user, 'Richard Best'. Is it possible to compare the full name is concatenated first name and last name? I do not have a full name field.
select * from users where last_name + ' ' + first_name like '%richa%'
I am using Mysql
These are equivalent:
select * from users where concat(last_name,' ',first_name) like '%richa%'
select * from users where concat_ws(' ',last_name,first_name) like '%richa%'
This might also work:
select * from users where last_name like '%richa%' or first_name like '%richa%'
Take a look at this thread.
Using mysql concat() in WHERE clause?
select * from users where (first_name + ' ' + last_name) like '%richa%'
In laravel Eloquent you can use whereRaw("concat(first_name,' ',last_name) LIKE %$search%")