How would I use a IN table with like? So that I could use % in them? By in I mean:
SELECT fields
FROM table
WHERE age = "50"
AND name IN ("tim", "bob", "nancy", "john");
I already tried:
SELECT fields
FROM table
WHERE age = "50"
AND name LIKE ("2010-09-17%", "2010-09-16%")
But it gave the error "Operand should contain 1 column(s)"
You can use a number of LIKE expressions:
SELECT fields
FROM table
WHERE age = "50"
AND (
name LIKE "2010-09-17%"
OR name LIKE "2010-09-16%"
);
or you can use a regex:
SELECT fields
FROM table
WHERE age = "50"
AND name REGEXP "2010-09-17.*|2010-09-16.*";
or, cleverly
SELECT fields
FROM table
WHERE age = "50"
AND name REGEXP "2010-09-1(6|7).*";
There is no combination of the LIKE and IN clauses. It's either one, or the other, syntax:
SELECT fields
FROM table
WHERE age = 50
AND ( name IN ('tim', 'bob', 'nancy', 'john')
OR name LIKE '2010-09-17%'
OR name LIKE '2010-09-16%')
The alternative to consider when searching text is Full Text Search (FTS):
SELECT fields
FROM table
WHERE age = 50
AND MATCH(name) AGAINST('tim bob nancy john')
...but this requires MyISAM tables, and Full Text Indexing.
Put the values in a table (MyParamsTable) and use LIKE in a JOIN condition e.g. something like:
SELECT fields
FROM table
INNER JOIN MyParamsTable
ON table.name LIKE CONCAT(MyParamsTable.name, "%")
WHERE age = "50";
Related
Say I have a table called "Team" with the following columns:
ID, MemberName ,ManagerName,Title
And I would like to retrieve all rows where a value "John" exists.
Assume "John" exists in a row for the MemberName column, and that "John" would exist in another row under the "ManagerName" column.
Please assume would have large number of columns. Greater than 50, and do would not know where the value would fall under statically.
If you need an exact match for "John" you can use following query:
Select *
From Team
Where MemberName = 'John' or ManagerName = 'John'
If you need all rows where "John" could be a part of the string then you can use like:
Select *
From Team
Where MemberName like '%John%' or ManagerName like '%John%'
Generally, you need to specify all the columns your are searching from in SQL.
SELECT * FROM Team WHERE 'John' IN (col1, col2, col3, ..., colN) ;
However that depends.
If you are using MySQL you can Search Table Data. From the MySQL Workbench right click the table , and choose Search Table Data.
If you are using PostgreSQL take a look at the following:
https://stackoverflow.com/a/52715388/10436747
Simple example: I have some (nearly) identical tables with personal data (age, name, weight, ...)
Now I have a simple, but long SELECT to find missing data:
Select ID
from personal_data_a
where
born is null
or age < 1
or weight > 500
or (name is 'John' and surname is 'Doe')
Now the problem is:
I have some personal_data tables where the column "surname" does not exit, but I want to use the same SQL-statement for all of them. So I have to check (inside the WHERE clause) that the last OR-condition is only used "IF the column surname exists".
Can it be done in a simple way?
You should have all people in the same table.
If you can't do that for some reason, consider creating a view. Something like this:
CREATE OR REPLACE VIEW v_personal_data
AS
SELECT id,
born,
name,
surname,
age,
weight
FROM personal_data_a
UNION ALL
SELECT id,
born,
name,
NULL AS surname, --> this table doesn't contain surname
age,
weight
FROM personal_data_b;
and then
SELECT id
FROM v_personal_data
WHERE born IS NULL
OR age < 1
OR ( name = 'John'
AND ( surname = 'Doe'
OR surname IS NULL))
Can it be done in a simple way?
No, SQL statements work with static columns and the statements will raise an exception if you try to refer to a column that does not exist.
You will either:
need to have a different query for tables with the surname column and those without;
have to check in the data dictionary whether the table has the column or not and then use dynamic SQL to build your query; or
to build a VIEW of the tables which do not have that column and add the column to the view (or add a GENERATED surname column with a NULL value to the tables that are missing it) and use that instead.
While dynamic predicates are usually best handled by the application or by custom PL/SQL objects that use dynamic SQL, you can solve this problem with a single SQL statement using DBMS_XMLGEN, XMLTABLE, and the data dictionary. The following code is not what I would call "simple", but it is simple in the sense that it does not require any schema changes.
--Get the ID column from a PERSONAL table.
--
--#4: Get the IDs from the XMLType.
select id
from
(
--#3: Convert the XML to an XMLType.
select xmltype(personal_xml) personal_xmltype
from
(
--#2: Convert the SQL to XML.
select dbms_xmlgen.getxml(v_sql) personal_xml
from
(
--#1: Use data dictionary to create SQL statement that may or may not include
-- the surname predicate.
select max(replace(replace(
q'[
Select ID
from #TABLE_NAME#
where
born is null
or age < 1
or weight > 500
or (name = 'John' #OPTIONAL_SURNAME_PREDICATE#)
]'
, '#TABLE_NAME#', table_name)
, '#OPTIONAL_SURNAME_PREDICATE#', case when column_name = 'SURNAME' then
'and surname = ''Doe''' else null end)) v_sql
from all_tab_columns
--Change this literal to the desired table.
where table_name = 'PERSONAL_DATA_A'
)
)
where personal_xml is not null
)
cross join xmltable
(
'/ROWSET/ROW'
passing personal_xmltype
columns
id number path 'ID'
);
See this db<>fiddle for a runnable example.
My query is something like this:
SELECT * FROM table WHERE name LIKE "%Mary%"
But now, I want bring all the occurrences as long as it's not contains "Jane" especially in "Mary". I do not want "Mary Jane", neither "Jane Mary" or any variation (e.g. "Mary Smith Jane").
I really don't know how to.
EDIT:
I'm not sure if I can only use a "not like" because I'm already using "not like" in the same query for other reasons.
In fact:
SELECT * FROM table WHERE name NOT LIKE "%John%"
AND name NOT LIKE '%Charlie%'
AND name LIKE '%Mary%'
Just add that to the WHERE clause:
WHERE name LIKE '%Mary%' AND
name NOT LIKE '%Mary Jane%'
Or, if you mean that the exact match is not what you want:
WHERE name LIKE '%Mary%' AND
name <> 'Mary Jane'
SELECT *
FROM your_table
WHERE name LIKE '%Mary%'
and name <> 'Mary Jane'
In order to retrieve records that contain Mary in the string, but not Jane, you will want to keep your clause for LIKE '%Mary%' and add a clause for NOT LIKE '%Jane%'.
You can group these clauses together with parenthesis in order to isolate them from other clauses in the query.
SELECT *
FROM table
WHERE
(name LIKE '%Mary%'
AND name NOT LIKE '%Jane%')
I have a table called "people" with a column named "name". I would like to select all rows where the name is "bob" or "john". I have tried the following and many variants of it, none of which work. How can I do this correctly?
select * from people where name is bob or john;
Thanks
To compare a column with a value you need to use = not IS
select *
from people
where name = 'bob'
or name = 'john';
Alternatively you can use the IN operator.
select *
from people
where name IN ('bob','john');
Note that string comparison is case-sensitive in SQL. So the above will not return rows where the name is Bob or John
I have a column which is called studentID, but I have millions of records and somehow the application has input some arbitrary text in the column.
How do I search:
SELECT *
FROM STUDENTS
WHERE STUDENTID CONTAINS TEXT
Leaving database modeling issues aside.
I think you can try
SELECT * FROM STUDENTS WHERE ISNUMERIC(STUDENTID) = 0
But ISNUMERIC returns 1 for any value that seems numeric including things like -1.0e5
If you want to exclude digit-only studentids, try something like
SELECT * FROM STUDENTS WHERE STUDENTID LIKE '%[^0-9]%'
Just try below script:
Below code works only if studentid column datatype is varchar
SELECT * FROM STUDENTS WHERE STUDENTID like '%Searchstring%'
Try LIKE construction, e.g. (assuming StudentId is of type Char, VarChar etc.)
select *
from Students
where StudentId like '%' || TEXT || '%' -- <- TEXT - text to contain
Try this:
SElECT * FROM STUDENTS WHERE LEN(CAST(STUDENTID AS VARCHAR)) > 0
With this you get the rows where STUDENTID contains text
riffing from bgs, please upvote them first.
I just wanted to expand on it
SELECT * FROM STUDENTS WHERE STUDENTID == 'Searchstring'
Will ONLY find Searchstring
SELECT * FROM STUDENTS WHERE STUDENTID like '%Searchstring%'
will find
1 Searchstring 1
2 Searchstring 2
3Searchstring3
etc Searchstring etc
SELECT * FROM STUDENTS WHERE STUDENTID like 'Searchstring%'
will find
Searchstring 1
Searchstring 2
SearchstringEtc
Will not find
1 Searchstring 1
or any prefixes at all
In this case % is used kinda the same as the same as the * wildcard, just for strings in this case.
Suppose STUDENTID contains some characters or numbers that you already know i.e. 'searchstring' then below query will work for you.
You could try this:
select * from STUDENTS where CHARINDEX('searchstring',STUDENTID)>0
I think this one is the fastest and easiest one.