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
Related
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
We have a query:
SELECT *
FROM wo_hdr
WHERE tm_no LIKE CONCAT('%', $P{tm_no}, '%')
However when the parameter $P{tm_no} is null we need to return all values.
I have seen others use
SELECT *
FROM wo_hdr
WHERE tm_no is null or tm_no LIKE CONCAT('%', $P{tm_no}, '%')
This does not work for us because then when we don't want to return null values when $P{tm_no} is not null.
Does anyone know a good solution for this? I thought possibly a CASE statement would work but it looked confusing to use one in the WHERE clause. It needs to work with both ORACLE and SQL Server.
Thank You.
Just modify your where clause with :
SELECT *
FROM wo_hdr
WHERE ($P{tm_no} IS NULL OR tm_no LIKE CONCAT('%', $P{tm_no}, '%'));
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.
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%'
I have a large database of names, and I'm hoping to identify incorrect capitalization.
Right now I'm using the following...
SELECT *
FROM myTable
WHERE LastName LIKE '%Mcd%' COLLATE SQL_Latin1_General_Cp1_CS_AS
Now of course this is inefficent because I have to run/edit this over and over for different cases. My thinking is find a list of name cases that would provide possible problems, and do
LIKE IN ('case1','case2','case3','case4', ...)
if that's possible. Is there another way that I'm not thinking of?
Other cases I'm thinking I'll have to check are abbreviations (%.%), hypens (%-%), and apostrophes (%'%).
You could use
SELECT *
FROM myTable
WHERE LastName LIKE '%Mcd%' or LastName LIKE '%Foo%'
Or
WITH T(Word) AS
(
SELECT 'Mcd' UNION ALL
SELECT 'Foo'
)
SELECT *
FROM myTable
JOIN T ON LastName LIKE '%' + Word + '%'
To avoid needing to scan myTable multiple times.
To avoid processing the string multiple times you could use CLR and Regular Expressions.