using % in IN operator of sql - sql

I have a query like this
select * from table_employee where name in ('Jack','Jon','Jade');
and it gives me three results.
but I want to use % with the names I mentioned in query like:
select * from table_employee where name in ('%Jack%','%Jon%','%Jade%');
The query execution is completed but now I get no results. Whats wrong here and how can i use % in IN operator?

You should use LIKE combined with OR, IN does not support wildcards
select * from table_employee where name LIKE '%Jack%'
OR name LIKE '%Jon%';

The in keyword does not work like that. You can use a like operand and bind the three values with or
example
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';

Convert it to an OR:
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';

If your fields data are exact what you want get in the result is no needed to use like . You should use like only when you don't know the data in a field but you know that it could contains a string Jack for example .
I'm sayng this because if you have an index on the field where you use LIKE it will be not used , so you will get performance problem .

Related

Compare two columns with 'LIKE' Operator in SQL / Laravel

I'm trying to compare two columns from a table. In which i have to check the email is containing his mobile number or not.
TableName:- Table1
TableColumns:- id,email,MOB
Ex.
SQL:
'SELECT * FROM Tabel1 WHERE email LIKE %MOB%'
Laravel :
Tabel1::whereColumn('email', 'LIKE','%MOB%')->get();
I have Tried this above query but it is showing syntax error.
Lets start by answering you question SQL-wise, anything in quotes is a literal to SQL, so you need to use column reference and add the wildcard symbols. You can do that like this:
SELECT * FROM Table1 WHERE email LIKE CONCAT('%', MOB, '%');
Now lets look at Laravel now, the 3rd argument to where expects a literal value not another column. You can overcome this via either whereRaw or DB::raw:
Table1::whereRaw("email LIKE CONCAT('%', MOB, '%')");
or
Table1::where('email', 'LIKE', DB::raw("CONCAT('%', MOB, '%')"));
You should try this
SELECT * FROM Tabel1 WHERE email LIKE '%' +MOB+'%'
SELECT * FROM `table` WHERE `email` LIKE '%MOB%';

SQL Server query on nvarchar with asterisk in data and in where clause

I have a table Product which looks like this:
Name (nvarchar(100), not null)
It has data that looks like this:
4503-U**F19
When I query like this:
select * from Product where Name = '4503-U**F19'
I get nothing back... what gives?
Is there some special way to escape a where clause that contains an asterisk?
I'm probably staring at the answer and can't see it.
Have you tried the N in the prefix?
select * from Product where Name = N'4503-U**F19'

Whats the right syntax for the string comparison query involving variable name in PostgreSQL?

Suppose I have a Table Name
First Name Last Name
--------------------------
Kris Kristos
I want a right syntax for the query which appears like
Select *
from Name
where First_name like '%'Last_Name'%'
You could use strpos:
SELECT *
FROM name
WHERE strpos(LastName, FirstName) > 0;
SqlFiddleDemo
I think I got the answer. 'concat' can be used for this purpose.
Select *
from name
where firstname like concat('%',lastname)

SQL search for string containing characters

I have a string "abc", and I want to search, in a SQL table, for the values that contain either a, b or c.
This is what I have right now:
Select * from TABLE where NAME like "abc" ;
This didn't work, but I know if I try something like
where Name like "a" or Name like "b" or ....
it will work.
Is there an easier way to do this?
Since I don't want to separate my string into characters.
You can use regular expression for this.
Have a look at the following :
Select * from TABLE where NAME REGEXP "[abc]";
select * from ab where name REGEXP '[abc]'
This will do
Select * from TABLE where NAME like "%abc%" ;
For more check information here http://www.techonthenet.com/sql/like.php

How to cast column within select query

I have this query:
SELECT * FROM students
WHERE name LIKE '%ka%' OR
tel LIKE '%12%' OR
address LIKE '%ka%';
but tel is an integer-type column and therefore I can't use the %abc% operator there (although I need to do it like that)
How can I cast the tel column within this query?
Try this
SELECT * FROM students
WHERE name LIKE '%ka%' OR
CAST(tel as text) LIKE '%12%' OR
address LIKE '%ka%';
Try this:
SELECT * FROM students
WHERE name LIKE '%ka%' OR
CONVERT(VARCHAR, tel) LIKE '%12%' OR
address LIKE '%ka%';
Convert the tel to a text type first
cast(tel as text) LIKE '%12%' OR
You should consider changing your column type, if you are not using an int type like an int
Indexes and other db optimizations are ineffective with a query like this.