Search with multiple criteria in LIKE query - sql

I have a problem when I work with SQL query. I have 2 table Student and Class, and I want to search by both student name, student id, class name, ...
If I write SQL Query like:
SELECT * FROM Student,Class
WHERE Student.classname = Class.classname
AND student_name LIKE '%ab%' OR class_name LIKE '%ab%'
It works well! But when I change %ab% part to ? to store value of textbox like this:
SELECT * FROM Student,Class
WHERE Student.classname = Class.classname
AND student_name LIKE ? OR class_name LIKE ?
It doesn't work and can not do anything. So, what can I do to compare multiple criteria with one input text in SQL?

I don't know what framework or tool you are using with your SQL, but given the following LIKE expression:
student_name LIKE ? OR class_name LIKE ?
if you wanted to find students or classes containing ab, then you would bind %ab% to both ? placeholders. You would do this from the tool.

I don't know what you want to have, but I guess you would like to declare a variable as one input?
Declare #input varchar(20)
Set #input = '%ab%'
SELECT * FROM Student,Class
WHERE Student.classname = Class.classname
AND student_name LIKE #input OR class_name LIKE #input

Do not use LIKE. Instead, use a regular expression.
There are only two benefits of LIKE:
under some circumstances, it is indexable, (your query is not)
and
LIKE is part of the standard, while regexps are DBMS dependent

Related

Create table - SQL Oracle

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.

using % in IN operator of 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 .

Compare strings in SQL

I am in a situation where I need to return results if some conditions on the string/character are met.
For example: to return only the names that contain 'F' character from the Person table.
How to create an SQL query based on such conditions? Is there any link to a documentation that explains how can SQL perform such queries?
Thanks in advance
The most basic approach is to use LIKE operator:
-- name starts with 'F'
SELECT * FROM person WHERE name LIKE 'F%'
-- name contains 'F'
SELECT * FROM person WHERE name LIKE '%F%'
(% is a wildcard)
Most RDBMS offer string operations which are able to perform that required task in one way or the other.
In MySQL you might use INSTR:
SELECT *
FROM yourtable
WHERE INSTR(Person, 'F') > 0;
In Oracle, this can be done, too.
In PostgreSQL, you can use STRPOS:
SELECT *
FROM yourtable
WHERE strpos(Person, 'F') > 0;
Usually there are several approaches to solve this, many would choose the LIKE operator. For more details, please refer to the documentation of the RDBMS of your choice.
Update
As requested by the questioner a few words about the LIKE operator, which are used not only in MySQL or Oracle, but in other RDBMS, too.
The use of LIKE will in some cases make your RDBMS try to use an index, it usually does not not try to do so if you use a string functions.
Example:
SELECT *
FROM yourtable
WHERE Person LIKE 'F%';
The query may look like this:
SELECT * FROM Person WHERE FirstName LIKE '%F%' OR LastName LIKE '%F%'

SQL Wildcards using %%%

I'm wondering what problems I may not foresee by using this query:
SELECT *
FROM tanswer
WHERE CourseID LIKE '%%%' AND Q39 LIKE 'p' AND Q42 LIKE 'a' AND Q43 LIKE 'a'
In particular using '%%%'.
The reason being, I have a dropdown that would send a '%' and in the sql I enclose it with the other two % to search through a set of courses, this query leaving the courseID searching through all courses.
The recordset returned seems to work fine, but I'm not sure if there's anything I may not be seeing by using three %%% together. Is this a special use of wildcards that I've stumbled across?
These are equivalent:
CourseID LIKE '%%%'
CourseID LIKE '%%'
CourseID LIKE '%'
Which is why your query behaves as you expected it to.
You need to use ! to escape the inner %: i.e. %!%%
see here

Getting records from two tables using Intersect

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.