Oracle select all rows using the contains keyword - sql

I'm looking for a character that I can use in an Oracle contains to get ALL results.
If I search for the string "test" in a title column I use this statement:
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">test</textquery></query>') > 0
With this statement I get the rows which have the "test"-string included in title-column.
BUT: Is there any way to use contains and select ALL rows?

Would this work for you?
select * from my_table
where title like
'<query><textquery grammar="CTXCAT">%</textquery></query>'
This uses the LIKE syntax and the % wildcard to achieve what I think you want.

The documentation says wildcards with the Oracle Text CONTAINS() predicate are % and _ just like the LIKE predicate.
Does the following work? I don't have an instance of Oracle handy to test.
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">%</textquery></query>') > 0

Related

How to use regexp_like for wildcard search in oracle?

I am using the below query to get Ids like 12766% and 39998%. How I can use regexp_like to check 12%66 and 39%98?
select * from table t where regexp_like( UPPER(ID),'12766|39998')
You may use the following regex pattern:
^(12[0-9]*66|39[0-9]*98)$
Oracle query:
SELECT *
FROM yourTable
WHERE REGEXP_LIKE(ID, '^(12[0-9]*66|39[0-9]*98)$');
Demo
But actually, you might just want to use LIKE here:
SELECT *
FROM yourTable
WHERE ID LIKE '12%66' OR ID LIKE '39%98';
This would work find, so long as you don't mind exactly what comes in between the digits.
I found solution for this. We can use the below query for a%d to match strings like abcd,acd,aabcd etc. A period character (.) is a perfect replacement for % in regexp which can support one or more occurrence of any characters supported in database.
select * from table where REGEXP_LIKE (UPPER (ID), '^12.66.|EY39.98.')

SQL: Return records containing a word where the last letter is anything except K

Suppose I have a table containing a column by the name "Query". I have to display the records where the string in this column has used noloc instead of nolock. Note that noloc can be followed/preceded by ) or space etc. I just need all records that have anything before and after noloc except nolock. Is there a way to do it in SQL?
I tried:
select * from table1
where Query LIKE '%noloc%'
but this includes queries containing nolock. I tried variations of the above like putting space before and/or after % but none of them fills all the criteria.
You can use both conditions in the where clause
select * from table1
where Query LIKE '%noloc%' and Query NOT LIKE '%nolock%'
You want anything + noloc + any one char but k + anything. Here:
select * from table1
where Query LIKE '%noloc[^k]%'

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.

select where substring

SQL newbie here, but I can't find the solution to something that looks easy:
The following query does not seem to have a valid syntax (ORA-00904: invalid identifier), but its logic should be clear. How can I achieve this in a query that needs to be speedy?
SELECT * FROM table WHERE LEFT(column,4)="abcd"
For this purpose, you should use like rather than left(). First, Oracle doesn't support left() (you need substr() instead). Second, like can make use of indexes because the wildcard is not at the beginning of the string:
SELECT *
FROM table
WHERE column like 'abcd%';
Oracle and some other products have substr.
SELECT * FROM tablename WHERE substr(columnname, 1, 4) = 'abcd'
I.e. single quotes for string literals!
ANSI SQL has substring:
SELECT * FROM tablename WHERE substring(columnname from 1 for 4) = 'abcd'
And others have left:
SELECT * FROM tablename WHERE LEFT(columnname,4) = 'abcd'

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