When im executing a command like
select last_name
from table
where last_name in('%a%e%','%e%a%') ;
to retrieve all those rows where the last name has an a and an e in it, I'm not getting the result
The in() clause does not support wildcards. Use like. Then you can simplify that to:
select last_name
from your_table
where last_name like '%a%'
and last_name like '%e%'
You should use LIKE statement when using wildcards
SELECT last_name
FROM table
WHERE last_name LIKE '%a%e%' OR last_name LIKE '%e%a%'
When you use IN it doesn't do wildcard expansion and searches for exact string you put there. In your case for strings '%a%e%' and '%e%a%' and not for strings where a and e are present.
you should use like not in
select last_name
from table
where last_name like '%a%e%'
or last_name like '%e%a%' ;
Related
I have a table named emp like this:
And I want to get the enames which start with 'A' or 'B'. So I write the query like this:
select ename from emp where ename like '[AB]%'
But it return nothing instead of 'ALLEN' or 'BLAKE'. So I changed the query into:
select ename from emp where ename REGEXP '[AB]%'
But it still didn't work and said there is no such function name REGEXP. Is there anything wrong ?
The following link may help you when using REGEXP. Sqlite3 supports REGEXP only
How do I use regex in a SQLite query?
However, I think the solution can be like this and no regex is need to be used:
select ename from emp where ename like 'A%' or ename like 'B%'
You have to add it as a user-defined function.
It would be simpler to just rewrite your query without regex. I.e.
select ename from emp where ename like 'A%' or ename like 'B%'
I need to create table, which should I call Others. I want only employeers who names which having names start with any other letter, but not K
I wrote sthg like this:
CREATE TABLE others AS select * from employees WHERE last_name no like 'K%';
I found sthg like this idea but it doesn't work
I'm receiving errror about syntax. Can you help me?
The second question: there is any other way to write it?
Try This
CREATE TABLE others AS (SELECT *
FROM employees
WHERE last_name NOT LIKE 'K%');
As #jarlh said in his comment, a view would serve the same purpose, but the data would only be stored once instead of twice, thus saving disk space. You could define the view as
CREATE OR REPLACE VIEW OTHERS AS
SELECT *
FROM EMPLOYEES
WHERE LAST_NAME NOT LIKE 'K%';
Best of luck.
I would recommend using just string functions. Here are two ways:
WHERE SUBSTR(last_name, 1, 1) <> 'K'
or:
WHERE last_name < 'K' or last_name >= 'L'
Although you can use LIKE or REGEXP_LIKE() for this, I like this simpler approaches.
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)
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.
I want to get List of students from a particular section and with other search criteria. I am doing this:-
declare #sectionId int, #name varchar
select #sectionId=23
select #name='a'
select UserId, FirstName from StudentMaster
Where FirstName Like #name and UserId IN
(
select UserId from StudentMaster
Intersect
select StudentId from StudentInSections where SectionId=#sectionId
)
but it is not giving right answer. If I only write Userid condition it works properly but I have to get list with whole search criteria.
is there anybody to help me?
The problem is the LIKE operand. If #name is 'a' this will only return students whose name is 'a' or 'A'. If you want student names starting with "a" you must add a wildcard "%"
FirstName LIKE 'a%'
(Some SQL dialects like MS Access use the wildcard "*" instead of "%".)
Note on case sensitive/insensitive search. Depending on the SQL dialect and the collation used for the column the search will be case sensitive or not. If you do not want to make a case sensitive search (i.e. you want to find students whose name starts with "a" or "A"), you can do this:
UPPER(FirstName) LIKE 'A%'
Or on the SQL-Server
FirstName COLLATE UTF8_GENERAL_CI LIKE '%a'
Where CI means case insensitive.
select sm.UserId, sm.FirstName from StudentMaster sm
inner join StudentInSections ss on ss.StudentId = sm.UserId
Where sm.FirstName Like #name
and ss.SectionId = #sectionId
Something like that should work. You just need to learn how to use inner joins.