SQL WildCards and LIKE - sql

I have table called INFO
ID Name
1 John
2 David
3 Kim
... ...
... ...
And I want to select Name with first letter D
SELECT Name FROM INFO
WHERE Name LIKE 'D%';
But I don't get any results. Why? What is wrong?

You should use * instead of %. see here for examples.
SELECT Name FROM INFO
WHERE Name LIKE 'D*';

Related

Sorting SQL query after search result

I have table that contains column FULL_NAME, and I'm doing search and getting result. I need to sort these columns by position of search(search term).
Example Of data
Column (FULL_NAME)
ID
FULL_NAME
1
zaid said Alabri
2
said sleem salim AlAhmedi
3
salim zaid Ahmed AlZaid
4
Ahmed said zaid AlSalimi
Search example:
SELECT FULL_NAME
FROM Table
WHERE(FULL_NAME LIKE N'%ِAhmed%')
I need result to be like this
ID
FULL_NAME
1
Ahmed said zaid AlSalimi
2
said Ahmed salim AlAhmedi
3
salim zaid Ahmed AlZaid
4
said sleem salim AlAhmedi
This what I'm seeking result sort by position after query result.
Since you have tagged SQL Server as your database please try this:
select *
from table
where full_name like N'%Ahmed%'
order by charindex('Ahmed',full_name,1)
Something like this could work for you:
select *
from table
where full_name like N'%Ahmed%'
order by charindex(N'Ahmed', full_name)
The charindex function returns the character number where the search matches a string.
I think if you are using SQL server you can use a conditional order by and using CHARIndex
as follow
SELECT EnglishName
FROM Employees
where EnglishName like N'%Ahmed%'
order by charindex(N'Ahmed',EnglishName,1)

sql multiple self joins efficient

I have a table that looks like the one below. I need to write a query to find all names that are present in all the sources. In the example below this is only "john" & "mark".
source name
a john
b john
c john
d john
a pat
b pat
a mark
b mark
c mark
d mark
Using the ALL statement, you can get the results you are expecting. Like this:
SELECT Name FROM dummy WHERE Source >= ALL (SELECT DISTINCT source FROM dummy);
See it in action: here
One method uses counting:
select name
from t
group by name
having count(distinct source) = (select count(distinct t2.source) from t t2);

select query for sql

Table description
Table Name:- Name condition
Name | Pattern
A | %A% or Name like %a%
B | %B% or Name like %b%
C | %C% or Name like %c%
D | %D% or Name like %d%
E | %E% or Name like %e%
F | %F% or Name like %f%
G | %G% or Name like %g%
Table name:- Employees
Emp_ID | EMP_NAME
1 | Akshay
2 | Akhil
3 | Gautam
4 | Esha
5 | bhavish
6 | Chetan
7 | Arun
[Table description] [1]: https://i.stack.imgur.com/wvOgr.png
Above are my two tables now my query is (in the image)
Select * from Employees,Name_condition where EMP_NAME like Pattern
Here the query is correct syntactically but produces wrong output.
It takes the column Pattern as a string and searches for it in EMP_NAME and it will find nothing.
So my question is how we can take the values present in the Pattern column as a condition and not as a string so that the query will become like this
Select * from Employees,Name_condition where EMP_NAME like ‘%A%’ or Name like ‘%a%’
what i need is when i pass colunm name(Pattern) in the where condition it takes %A% or Name like %a% whole as a string but i want that select * from Employees,Name_condition where EMP_NAME like Pattern Here the column name pattern internally must be replace by the value present in the column and the the query produces o/p like this
Select * from Employees,Name_condition where EMP_NAME like ‘%A%’ or Name like ‘%a%’
Desired Result:-I expect all the rows in my result which includes bhavish but as we see we have a like condition in the column itself like %B% or Name like %b%
What i want is when it matches
where EMP_NAME like Pattern
The value of pattern must internally replaced by
%B% or Name like %b%
and the it produces the output which includes bhavish which starts with b
try:
select *
from employees
where emp_name like '%oh%'
or emp_name like '%a%';
Good luck.
Try this from orafaq:
SELECT * FROM employees
WHERE emp_name LIKE '\%a\%' ESCAPE '\';
It's not that simple (and it shouldn't be). If you really have to use such tables you have to write a piece of PL/SQL to handle your conditions.
Two things you have to read about:
dynamic sql
sql injection (because you want to prevent it)
Try to Put && instead of 'And' in condition

How to use LIKE in a query to find multiple words?

I have a cust table
id name class mark
1 John Deo Matt Four 75
2 Max Ruin Three 85
3 Arnold Three 55
4 Krish Star HN Four 60
5 John Mike Four 60
6 Alex John Four 55
I would like to search for a customer which might be given as John Matt without the deo string. How to use a LIKE condition for this?
SELECT * FROM cust WHERE name LIKE '%John Matt%'
The result should fetch the row 1.
what if the search string is Matt Deo or john
The above can't be implemented when trying to find an exact name. How can I make the LIKE query to fetch the customer even if 2 strings are given?
If the pattern to be matched is
string1<space>anything<space>string2
you can write:
like string1||' % '||string2
Why not this
select * from cust where name Like 'John%Matt' ;
SELECT *
FROM custtable
WHERE upper(NAME) LIKE '%' || upper(:first_word) || '%'
AND upper(NAME) LIKE '%' || upper(:second_word) || '%'
Must you use LIKE? Oracle has plenty of more powerful search options.
http://docs.oracle.com/cd/B19306_01/server.102/b14220/content.htm#sthref2643
I'd look at those.
I believe you need REGEXP_LIKE( ):
SQL> with tbl(name) as (
select 'John Deo Matt' from dual
)
select name
from tbl
where regexp_like(name, 'matt|deo', 'i');
NAME
-------------
John Deo Matt
SQL>
Here the regex string specifies name contains 'matt' OR 'deo' and the 'i' means its case-insensitive. The order of the names does not matter.

SQL - compare part of word in WHERE clausule

I have problem with sql query.
For example, I have table like this:
ID Name1 Name2 Country
1 Greg Torr Poland
2 John Smith England
3 Tom Jerry USA
I want get all record, which have for example, "la" in Country. In this case:
PoLAnd
EngLAnd
How I can put this in Where clausule?
Greets
Use the LIKE keyword:
SELECT * FROM table
WHERE Country LIKE '%la%'
I think you can use the Like Clause here. The examples are available on
http://www.sql-tutorial.net/SQL-LIKE.asp
http://www.w3schools.com/sql/sql_like.asp
etc.
WHERE CHARINDEX('LA', Country) > 0
alternatively
WHERE Country LIKE '%la%'
If your RDBMS is case-sensitive, convert Country to upper case using the appropriate string function, and compare against the upper case LA with a LIKE:
SELECT *
FROM tbl
WHERE UPPER(Country) LIKE '%LA%'
Enclose your keyword before and after with Percent Symbol
SELECT *
FROM tableName
WHERE Country LIKE '%LA%'