Oracle: Mixing IN and LIKE, is it possible? [duplicate] - sql

This question already has answers here:
How can I introduce multiple conditions in LIKE operator?
(10 answers)
Closed 8 years ago.
Is it possible to do someting like this?
Ie.
IN ('%1', '%2')
The values are not few
How would you solve this task?

You could also use REGEXP_LIKE instead of LIKE. It can be more flexible in this type of situation.
To get everything that ends with '1' or '2' just use:
SELECT * FROM table_name WHERE REGEXP_LIKE(col_name, '[12]$')
The [12] means match 1 or 2. The $ means match the end of the string.
If you need something such as this:
LIKE '%1' OR LIKE '%2' or LIKE '%A' or LIKE '%W'
... you just have to add the other characters within the square brackets:
SELECT * FROM table_name WHERE REGEXP_LIKE(col_name, '[12AW]$')
Or, if you wanted anything that ends with a digit you could use this:
SELECT * FROM table_name WHERE REGEXP_LIKE(col_name, '[[:digit:]]$'

If you have only a few, just combine them with or:
column like '%1' or column like '%2' or...
If you have a lot, you can create a table with the patterns and join to it.
create table matches (
match char(10)
);
insert into matches values
('%1'),('%2'),...;
select * from table
inner join matches on table.column like matches.match;
That has some issues with duplicate rows if a single row matches more than one pattern, but you can modify it depending on what kind of final output you want.
As a third option, you could use substr() rather than like if they are all the same length:
select * from table where substr(column,-1,1) in ('1','2',...)
This checks if the last character is in the set of values.

How about using like itself?
Column LIKE '%1' OR Column LIKE '%2'

Basically same as examples of others but closest to your example:
SELECT deptno, empno, ename FROM scott.emp
WHERE job IN
(SELECT job FROM scott.emp WHERE job Like ('SALE%') OR job Like ('MANAG%'))
/

Related

How to perform Like in SQL on a column with '%' in the data?

I am using oracle database and writing the likeness filter for the persistence layer and want to perform likeness on a column that can possibly have '%' in it's data.
To filter data I am writing the query for likeness using LIKE clause as
select * from table where columnName like '%%%';
which is returning all the values but I only want the rows that contains '%' in the columnName.
Not sure what escape character to use or what to do to filter on the '%' symbol. Any suggestions??
Also, I have to do the same thing using Criteria api in java and have no clues about putting escape character there.
You can use an escape character.
where columnName like '%$%%' escape '$'
REGEXP_LIKE might help in a rather simple manner.
SQL> with test (col) as
2 (select 'abc%def' from dual union all
3 select '%12345&' from dual union all
4 select '%abc12%' from dual union all
5 select '1234567' from dual
6 )
7 select *
8 from test
9 where regexp_like(col, '%');
COL
-------
abc%def
%12345&
%abc12%
SQL>
If I have understood it correctly the answer from the #Littlefoot is correct(and I will up-vote it now). I will just add more details because you are looking for name of the columns of your table "I only want the rows that contains '%' in the columnName".
Here is the table I have created:
CREATE TABLE "table_WITH%" ("numer%o" number
, name varchar2(50)
, "pric%e" number
, coverage number
, activity_date date);
Then this query gives me the correct answer:
SELECT column_name
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'table_WITH%'
AND regexp_like(COLUMN_NAME, '%');
Gordon's answer using the escape clause of the like command is correct in the general case.
In your specific case (searching for a single character, anywhere in a string), a simpler method is to use instr, e.g.
where instr(columnname, '%') > 0

select TableData where ColumnData start with list of strings

Following is the query to select column data from table, where column data starts with a OR b OR c. But the answer i am looking for is to Select data which starts with List of Strings.
SELECT * FROM Table WHERE Name LIKE '[abc]%'
But i want something like
SELECT * FROM Table WHERE Name LIKE '[ab,ac,ad,ae]%'
Can anybody suggest what is the best way of selecting column data which starts with list of String, I don't want to use OR operator, List of strings specifically.
The most general solution you would have to use is this:
SELECT *
FROM Table
WHERE Name LIKE 'ab%' OR Name LIKE 'ac%' OR Name LIKE 'ad%' OR Name LIKE 'ae%';
However, certain databases offer some regex support which you might be able to use. For example, in SQL Server you could write:
SELECT *
FROM Table
WHERE NAME LIKE 'a[bcde]%';
MySQL has a REGEXP operator which supports regex LIKE operations, and you could write:
SELECT *
FROM Table
WHERE NAME REGEXP '^a[bcde]';
Oracle and Postgres also have regex like support.
To add to Tim's answer, another approach could be to join your table with a sub-query of those values:
SELECT *
FROM mytable t
JOIN (SELECT 'ab' AS value
UNION ALL
SELECT 'ac'
UNION ALL
SELECT 'ad'
UNION ALL
SELECT 'ae') v ON t.vame LIKE v.value || '%'

How to use multiple values with like in sql

select * from user_table where name in ('123%','test%','dummy%')
How to ensure that this where clause is not an exact match, but a like condition?
In Oracle you can use regexp_like as follows:
select *
from user_table
where regexp_like (name, '^(123|test|dummy)')
The caret (^) requires that the match is at the start of name, and the pipe | acts as an OR.
Be careful though, because with regular expressions you almost certainly lose the benefit of an index that might exist on name.
Use like this,
select *
from user_table
where name LIKE '123%'
OR name LIKE 'test%'
OR name Like 'dummy%';
another option in MySQL
select * from user_table where name REGEXP '^123|^test|^dummy';
To not lose indexed access to rows in Oracle a table collection expression can be used:
SELECT
*
FROM
user_table
JOIN (SELECT column_value filter
FROM table(sys.odcivarchar2list('dummy%', '123%', 'test%'))
) ON user_table.name LIKE filter
The filter expressions must be distinct otherwise you get the same rows from user_table multiple times.

Combining "LIKE" and "IN" for SQL Server [duplicate]

This question already has answers here:
Is there a combination of "LIKE" and "IN" in SQL?
(28 answers)
Closed 9 years ago.
Is it possible to combine LIKE and IN in a SQL Server-Query?
So, that this query
SELECT * FROM table WHERE column LIKE IN ('Text%', 'Link%', 'Hello%', '%World%')
Finds any of these possible matches:
Text, Textasd, Text hello, Link2, Linkomg, HelloWorld, ThatWorldBusiness
etc...
Effectively, the IN statement creates a series of OR statements... so
SELECT * FROM table WHERE column IN (1, 2, 3)
Is effectively
SELECT * FROM table WHERE column = 1 OR column = 2 OR column = 3
And sadly, that is the route you'll have to take with your LIKE statements
SELECT * FROM table
WHERE column LIKE 'Text%' OR column LIKE 'Hello%' OR column LIKE 'That%'
I know this is old but I got a kind of working solution
SELECT Tbla.* FROM Tbla
INNER JOIN Tblb ON
Tblb.col1 Like '%'+Tbla.Col2+'%'
You can expand it further with your where clause etc.
I only answered this because this is what I was looking for and I had to figure out a way of doing it.
One other option would be to use something like this
SELECT *
FROM table t INNER JOIN
(
SELECT 'Text%' Col
UNION SELECT 'Link%'
UNION SELECT 'Hello%'
UNION SELECT '%World%'
) List ON t.COLUMN LIKE List.Col
No, you will have to use OR to combine your LIKE statements:
SELECT
*
FROM
table
WHERE
column LIKE 'Text%' OR
column LIKE 'Link%' OR
column LIKE 'Hello%' OR
column LIKE '%World%'
Have you looked at Full-Text Search?
No, MSSQL doesn't allow such queries. You should use col LIKE '...' OR col LIKE '...' etc.

Is there any way to combine IN with LIKE in an SQL statement?

I am trying to find a way, if possible, to use IN and LIKE together. What I want to accomplish is putting a subquery that pulls up a list of data into an IN statement. The problem is the list of data contains wildcards. Is there any way to do this?
Just something I was curious on.
Example of data in the 2 tables
Parent table
ID Office_Code Employee_Name
1 GG234 Tom
2 GG654 Bill
3 PQ123 Chris
Second table
ID Code_Wildcard
1 GG%
2 PQ%
Clarifying note (via third-party)
Since I'm seeing several responses which don't seems to address what Ziltoid asks, I thought I try clarifying what I think he means.
In SQL, "WHERE col IN (1,2,3)" is roughly the equivalent of "WHERE col = 1 OR col = 2 OR col = 3".
He's looking for something which I'll pseudo-code as
WHERE col IN_LIKE ('A%', 'TH%E', '%C')
which would be roughly the equivalent of
WHERE col LIKE 'A%' OR col LIKE 'TH%E' OR col LIKE '%C'
The Regex answers seem to come closest; the rest seem way off the mark.
I'm not sure which database you're using, but with Oracle you could accomplish something equivalent by aliasing your subquery in the FROM clause rather than using it in an IN clause. Using your example:
select p.*
from
(select code_wildcard
from second
where id = 1) s
join parent p
on p.office_code like s.code_wildcard
In MySQL, use REGEXP:
WHERE field1 REGEXP('(value1)|(value2)|(value3)')
Same in Oracle:
WHERE REGEXP_LIKE(field1, '(value1)|(value2)|(value3)')
Do you mean somethign like:
select * FROM table where column IN (
SELECT column from table where column like '%%'
)
Really this should be written like:
SELECT * FROM table where column like '%%'
Using a sub select query is really beneficial when you have to pull records based on a set of logic that you won't want in the main query.
something like:
SELECT * FROM TableA WHERE TableA_IdColumn IN
(
SELECT TableA_IdColumn FROM TableB WHERE TableA_IDColumn like '%%'
)
update to question:
You can't combine an IN statement with a like statement:
You'll have to do three different like statements to search on the various wildcards.
You could use a LIKE statement to obtain a list of IDs and then use that in the IN statement.
But you can't directly combine IN and LIKE.
Perhaps something like this?
SELECT DISTINCT
my_column
FROM
My_Table T
INNER JOIN My_List_Of_Value V ON
T.my_column LIKE '%' + V.search_value + '%'
In this example I've used a table with the values for simplicity, but you could easily change that to a subquery. If you have a large list (like tens of thousands) then performance might be rough.
select *
from parent
where exists( select *
from second
where office_code like trim( code_wildcard ) );
Trim code_wildcard just in case it has trailing blanks.
You could do the Like part in a subquery perhaps?
Select * From TableA Where X in (Select A from TableB where B Like '%123%')
tsql has the contains statement for a full-text-search enabled table.
CONTAINS(Description, '"sea*" OR "bread*"')
If I'm reading the question correctly, we want all Parent rows that have an Office_code that matches any Code_Wildcard in the "Second" table.
In Oracle, at least, this query achieves that:
SELECT *
FROM parent, second
WHERE office_code LIKE code_wildcard;
Am I missing something?