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%'
Related
I am writing an SQL query to select each row except those which have mentioned city values.
But somehow it's not working and selects rows with these cities.
Select * from Emp
Where City not in ('Suart', 'Vapi');
Upcoming output
Name City
Kris Surat
Joy vapi
Riva Goa
Jeni Mumbai
Maya Sayan
Expected output
Name City
Riva Goa
Jeni Mumbai
Maya Sayan
Maybe your data must be case sensitive or spaces added.
Try this if it works
Select * from Emp
Where lower(ltrim(rtrim(City))) not in ('suart', 'vapi');
Or
Select * from Emp
Where lower(ltrim(rtrim(City))) not like '%suart%' OR
lower(ltrim(rtrim(City))) not like '%vapi%'
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)
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.
I have a table similar to the below:
id | name | direction |
--------------------------------------
1 Jhon Washington, DC
2 Diego Miami, Florida
3 Michael Orlando, Florida
4 Jenny Olympia, washington
5 Joe Austin, Texas
6 Barack Denver, Colorado
and I want to count how many people live in a specific state:
Washington 2
Florida 2
Texas 1
Colorado 1
How can I do this? (By the way this is just an question with an academic point of view )
Thanks in advance!
Postgres offers the function split_part(), which will break up a string by a delimiter. You want the second part (the part after the comma):
select split_part(direction, ', ', 2) as state, count(*)
from t
group by split_part(direction, ', ', 2);
Initially I would obtain the state from the direction field. Once you have that, it's quite simple:
SELECT state, count(*) as total FROM initial_table group by state.
To obtain the state, some functions depending on the dbms are useful. It depends on the language.
A possible pseudocode (given a function like substring_index of MySQL) for the query would be:
SELECT substring_index(direction,',',-1) as state, count(*) as total
FROM initial_table group by substring_index(direction,',',-1)
Edit: As it is suggested above, the query should return 1 for the Washington state.
My way do making such a queries is two-step - first, prepare fields you need, second, do you grouping or other calculation. That way you're following DRY principle and don't repeating yourself. I think CTE is the best tool for this:
with cte as (
-- we don't need other fields, only state
select
split_part(direction, ', ', 2) as state
from table1
)
select state, count(*)
from cte
group by state
sql fiddle demo
If you writing queries that way, it's easy to change grouping field in the future.
Hope that helps, and remember - readability counts! :)
Can I use a table column within a Like operator? I've created an example,
TableA
Names Location
Albert Smith Senior Aberdeen
John Lee London
Michael Rogers Junior Newcastle
Mary Roberts Edinburgh
TableB
Names
Albert Smith
John Lee
Michael Rogers
I want to do a query such as:
SELECT TableA.Location
into NewTable
FROM TableA
WHERE TableA.Names Like '*[TableB.Names]*';
In this case, there would be no match for Mary Roberts, Edinburgh but the first three locations would be returned.
Is it possible to put a column into a like statement?
If not does anyone have any ideas how I could do this?
Hope you can help
PS I can't use an actual asterisk since this is removed and the text italicised, also I have read about using % instead but this has not worked for me.
You can join the two tables and use LIKE within the JOIN clause:
SELECT TableA.Location
into NewTable
FROM TableA
INNER JOIN TableB ON TableA.Names LIKE TableB.Names & '*';
Honestly, I had no idea that you can do this in Access before I tried it just now :-)