How to use multiple values with like in sql - 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.

Related

Alias result use into where clause

Hello I want to use alias result as where clause in SQL query.
Following is my SQL query.
SELECT SQL_CALC_FOUND_ROWS
`id`,
`number`,
`ref_no`,
CONCAT(" cosid LIKE '%",REPLACE(abc_cosing, ',',"%' OR cosid LIKE '%"),"%'") AS TEST,
(SELECT COUNT(id)
FROM `tbl_data_pub`
WHERE TEST ) AS TOTALREC FROM `tbl_data_main` as aa LEFT JOIN tbl_ref_table as bb on aa.id=bb.abc_id WHERE ref_no='12345'
And I am getting value of TEST as "12345%' OR cosid LIKE '%32837%' OR cosi_..." and I want to this in where clause for like. But how can I?
try this:
select * from
(
SELECT SQL_CALC_FOUND_ROWS
`ing_id`,
`ing_cas_no`,
`ing_cosing_ref_no`,
REPLACE(aig_addi_all_cosing, ',',"%' OR usf_eu_cosing_id LIKE '%") AS TEST,
(SELECT COUNT(usf_id)
FROM `tbl_USFDA_published`
WHERE (usf_eu_cosing_id LIKE TEST)) AS USFDAREG
FROM `tbl_ingredients` as aa
LEFT JOIN tbl_ing_addi_cosing_ref as bb on aa.ing_id=bb.aig_ing_id
WHERE ing_cosing_ref_no='38617'
)
where TEST like '%xxx%'
You can get the result from using CTE. 'Where' clause dont allow alias. Try this below query it will help.
;WIth CTE as (SELECT SQL_CALC_FOUND_ROWS
`ing_id`,
`ing_cas_no`,
`ing_cosing_ref_no`,
REPLACE(aig_addi_all_cosing, ',',"%' OR usf_eu_cosing_id LIKE '%") AS TEST,
(SELECT COUNT(usf_id)
FROM `tbl_USFDA_published`
WHERE (usf_eu_cosing_id LIKE TEST)) AS USFDAREG
FROM `tbl_ingredients` as aa
LEFT JOIN tbl_ing_addi_cosing_ref as bb on aa.ing_id=bb.aig_ing_id
WHERE ing_cosing_ref_no='38617')
select * from CTE
where TEST like '38617%'
MySQL extends the use of the having clause to behave like a where in a non-aggregation query. You can use this:
SELECT SQL_CALC_FOUND_ROWS ing_id, ing_cas_no, ing_cosing_ref_no,
REPLACE(aig_addi_all_cosing, ',',"%' OR usf_eu_cosing_id LIKE '%") AS TEST,
(SELECT COUNT(usf_id)
FROM `tbl_USFDA_published`
WHERE (usf_eu_cosing_id LIKE TEST)) AS USFDAREG
FROM tbl_ingredients i LEFT JOIN
tbl_ing_addi_cosing_ref acr
ON i.ing_id = acr.aig_ing_id
WHERE ing_cosing_ref_no = 38617
HAVING test LIKE '%AAA%';
There are good reasons why MySQL has this extension. Two reasons are that subqueries are not allowed in the FROM clause for views. And, MySQL has a tendency to materialize subqueries -- which adds extra overhead -- and this avoids that overhead.
Some other notes:
Do not use backticks unnecessarily. You don't need to escape the names of your columns or tables, so don't bother.
Use meaningful table aliases, not random letters. That means abbreviations for the table names.
Qualify all column names in the query. I haven't done this, because I have no idea where the columns are coming from.
Presumably ing_cosing_ref_no is a number. Compare the value to a number not a string.

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 || '%'

Using CONTAINS to find items IN a table

I'm trying to write a SP that will allow users to search on multiple name strings, but supports LIKE functionality. For example, the user's input might be a string 'Scorsese, Kaurismaki, Tarkovsky'. I use a split function to turn that string into a table var, with one column, as follows:
part
------
Scorsese
Kaurismaki
Tarkovsky
Then, normally I would return any values from my table matching any of these values in my table var, with an IN statement:
select * from myTable where lastName IN (select * from #myTableVar)
However, this only returns exact matches, and I need to return partial matches. I'm looking for something like this, but that would actually compile:
select * from myTable where CONTAINS(lastName, select * from #myTableVar)
I've found other questions where it's made clear that you can't combine LIKE and IN, and it's recommended to use CONTAINS. My specific question is, is it possible to combine CONTAINS with a table list of values, as above? If so, what would that syntax look like? If not, any other workarounds to achieve my goal?
I'm using SQL Server 2016, if it makes any difference.
You can use EXISTS
SELECT * FROM myTable M
WHERE
EXISTS( SELECT * FROM #myTableVar V WHERE M.lastName like '%'+ V.part +'%' )
Can your parser built the entire statement? Will that get you what you want?
select *
from myTable
where CONTAINS
(lastName,
'"Scorsese" OR "Kaurismaki" OR "Tarkovsky"'
)
This can be done using CHARINDEX function combined with EXISTS:
select *
from myTable mt
where exists(select 1 from #myTableVar
where charindex(mt.lastName, part) > 0
or charindex(part, mt.lastName) > 0)
You might want to omit one of the conditions in the inner query, but I think this is what you want.

sql In clause with wildcards (DB2)

Is there anyway to use wildcards in a clause similar to a "in", like this
select * from table where columnx xxxxxxx ('%a%','%b%')?
I know I can do:
select * from table where (columnx like '%a%' or columnx like '%b%')
But I'm looking for an alternative to make the querystring shorter.
BTW: I'm not able to register any custom functions, nor temp tables, it should be a native DB2 function.
I found this similar answer for oracle and SQLServer:
Is there a combination of "LIKE" and "IN" in SQL?
There's no native regular expression support in "pureSQL" for DB2, you can either create your own as in:
http://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html
or use pureXML as in: http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
Example:
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
Yet another variant that may be shorter:
select t.*
from table t
join ( values '%a%', '%b%' ) u (columnx)
on t.columnx like u.columnx

Using LIKE in an Oracle IN clause

I know I can write a query that will return all rows that contain any number of values in a given column, like so:
Select * from tbl where my_col in (val1, val2, val3,... valn)
but if val1, for example, can appear anywhere in my_col, which has datatype varchar(300), I might instead write:
select * from tbl where my_col LIKE '%val1%'
Is there a way of combing these two techniques. I need to search for some 30 possible values that may appear anywhere in the free-form text of the column.
Combining these two statements in the following ways does not seem to work:
select * from tbl where my_col LIKE ('%val1%', '%val2%', 'val3%',....)
select * from tbl where my_col in ('%val1%', '%val2%', 'val3%',....)
What would be useful here would be a LIKE ANY predicate as is available in PostgreSQL
SELECT *
FROM tbl
WHERE my_col LIKE ANY (ARRAY['%val1%', '%val2%', '%val3%', ...])
Unfortunately, that syntax is not available in Oracle. You can expand the quantified comparison predicate using OR, however:
SELECT *
FROM tbl
WHERE my_col LIKE '%val1%' OR my_col LIKE '%val2%' OR my_col LIKE '%val3%', ...
Or alternatively, create a semi join using an EXISTS predicate and an auxiliary array data structure (see this question for details):
SELECT *
FROM tbl t
WHERE EXISTS (
SELECT 1
-- Alternatively, store those values in a temp table:
FROM TABLE (sys.ora_mining_varchar2_nt('%val1%', '%val2%', '%val3%'/*, ...*/))
WHERE t.my_col LIKE column_value
)
For true full-text search, you might want to look at Oracle Text: http://www.oracle.com/technetwork/database/enterprise-edition/index-098492.html
A REGEXP_LIKE will do a case-insensitive regexp search.
select * from Users where Regexp_Like (User_Name, 'karl|anders|leif','i')
This will be executed as a full table scan - just as the LIKE or solution, so the performance will be really bad if the table is not small. If it's not used often at all, it might be ok.
If you need some kind of performance, you will need Oracle Text (or some external indexer).
To get substring indexing with Oracle Text you will need a CONTEXT index. It's a bit involved as it's made for indexing large documents and text using a lot of smarts. If you have particular needs, such as substring searches in numbers and all words (including "the" "an" "a", spaces, etc) , you need to create custom lexers to remove some of the smart stuff...
If you insert a lot of data, Oracle Text will not make things faster, especially if you need the index to be updated within the transactions and not periodically.
No, you cannot do this. The values in the IN clause must be exact matches. You could modify the select thusly:
SELECT *
FROM tbl
WHERE my_col LIKE %val1%
OR my_col LIKE %val2%
OR my_col LIKE %val3%
...
If the val1, val2, val3... are similar enough, you might be able to use regular expressions in the REGEXP_LIKE operator.
Yes, you can use this query (Instead of 'Specialist' and 'Developer', type any strings you want separated by comma and change employees table with your table)
SELECT * FROM employees em
WHERE EXISTS (select 1 from table(sys.dbms_debug_vc2coll('Specialist', 'Developer')) mt
where em.job like ('%' || mt.column_value || '%'));
Why my query is better than the accepted answer: You don't need a CREATE TABLE permission to run it. This can be executed with just SELECT permissions.
In Oracle you can use regexp_like as follows:
select *
from table_name
where regexp_like (name, '^(value-1|value-2|value-3....)');
The caret (^) operator to indicate a beginning-of-line character &
The pipe (|) operator to indicate OR operation.
This one is pretty fast :
select * from listofvalue l
inner join tbl on tbl.mycol like '%' || l.value || '%'
Just to add on #Lukas Eder answer.
An improvement to avoid creating tables and inserting values
(we could use select from dual and unpivot to achieve the same result "on the fly"):
with all_likes as
(select * from
(select '%val1%' like_1, '%val2%' like_2, '%val3%' like_3, '%val4%' as like_4, '%val5%' as like_5 from dual)
unpivot (
united_columns for subquery_column in ("LIKE_1", "LIKE_2", "LIKE_3", "LIKE_4", "LIKE_5"))
)
select * from tbl
where exists (select 1 from all_likes where tbl.my_col like all_likes.united_columns)
I prefer this
WHERE CASE WHEN my_col LIKE '%val1%' THEN 1
WHEN my_col LIKE '%val2%' THEN 1
WHEN my_col LIKE '%val3%' THEN 1
ELSE 0
END = 1
I'm not saying it's optimal but it works and it's easily understood. Most of my queries are adhoc used once so performance is generally not an issue for me.
select * from tbl
where exists (select 1 from all_likes where all_likes.value = substr(tbl.my_col,0, length(tbl.my_col)))
You can put your values in ODCIVARCHAR2LIST and then join it as a regular table.
select tabl1.* FROM tabl1 LEFT JOIN
(select column_value txt from table(sys.ODCIVARCHAR2LIST
('%val1%','%val2%','%val3%')
)) Vals ON tabl1.column LIKE Vals.txt WHERE Vals.txt IS NOT NULL
You don't need a collection type as mentioned in https://stackoverflow.com/a/6074261/802058. Just use an subquery:
SELECT *
FROM tbl t
WHERE EXISTS (
SELECT 1
FROM (
SELECT 'val1%' AS val FROM dual
UNION ALL
SELECT 'val2%' AS val FROM dual
-- ...
-- or simply use an subquery here
)
WHERE t.my_col LIKE val
)