REGEXP_LIKE Oracle function - sql

I have a list of 100 words that I need to do a pattern match on 55 Million rows of data. Is there a way to create a list of words and pass the list through the REGEXP_LIKE function, instead of using the | (or) statement multiple times, can a list be input instead?
Search *
From table
Where REGEXP_LIKE(C1, 'wordlword2letc...', 'i');

You cannot pass a list of words as pattern in REGEXP_LIKE.
pattern is the regular expression and usually is text literal and cannot be more than 512 bytes.
What you can possibly do is, store the words you're trying to search in separate table/column and then use LIKE condition in your query as you're just trying to search for the occurrence of the words and not expecting regular expression search support.
So, if there is a table/column (new_table.col) which stores your input items to search for, your query might look like (using UPPER function to ensure case insensitive search as you were trying) -
SELECT a.* FROM table a, new_table b WHERE UPPER(a.col1) LIKE UPPER(b.col);

Related

SQL String Pattern Matching

I am trying to run a simple select query on a table to exclude all the records containing a single word on Snowflake.
For E.g. there is a column - " NAME" having datatype - STRING - containing a single word and combination of words.
I want to build a query to exclude all iterations of a sub-string present in that column ( uppercase,lowercase and camel-case via a single query)
I am trying to run a simple select query on a table to exclude all the records containing a single word
You could use ILIKE to perform case-insensitive comparison:
SELECT *
FROM tab
WHERE NAME NOT ILIKE '%phrase%';
db<>fiddle demo

Is there a way to do a word search for values of one column in another column?

I have a database that contains columns that are text fields (strings, a few sentences long) and columns that are shorter strings (eg: college majors). Is there any way I can use the 'LIKE' function in SQL to search whether one of the values of the College Major column appears in another column?
I don't want to write out each of the college majors as a string since there are over 100.
Yes you can. Something like
where bigdatacolumn like '%' + computer_major + '%'
Since you said, other column contains few lines (Text column), you probably want to consider using Full Text Search instead of using LIKE operator
Use The ANY operator to compare in an array as shown in the below query.
select * from staff where department ILIKE ANY ( ARRAY['AUT%', '%COM%' ,'SP%' ] :: TEXT[] );

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

Finding the "&" character in SQL SERVER using a like statement and Wildcards

I need to find the '&' in a string.
SELECT * FROM TABLE WHERE FIELD LIKE ..&...
Things we have tried :
SELECT * FROM TABLE WHERE FIELD LIKE '&&&'
SELECT * FROM TABLE WHERE FIELD LIKE '&\&&'
SELECT * FROM TABLE WHERE FIELD LIKE '&|&&' escape '|'
SELECT * FROM TABLE WHERE FIELD LIKE '&[&]&'
None of these give any results in SQLServer.
Well some give all rows, some give none.
Similar questions that didn't work or were not specific enough.
Find the % character in a LIKE query
How to detect if a string contains special characters?
some old reference Server 2000
http://web.archive.org/web/20150519072547/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-search-for-special-characters-e-g-in-sql-server.html
& isn't a wildcard in SQL, therefore no escaping is needed.
Use % around the value your looking for.
SELECT * FROM TABLE WHERE FIELD LIKE '%&%'
Your statement contains no wildcards, thus is equivalent to WHERE FIELD = '&'.
& isn't a special character in SQL so it doesn't need to be escaped. Just write
WHERE FIELD LIKE '%&%'
to search for entries that contain & somewhere in the field
Be aware though, that this will result in a full table scan as the server can't use any indexes. Had you typed WHERE FIELD LIKE '&%' the server could do a range seek to find all entries starting with &.
If you have a lot of data and can't add any more constraints, you should consider using SQL Server's full-text search to create and use and FTS index, with predicates like CONTAINS or FREETEXT

Specific word only with CONTAINS, FREETEXT?

How do I search for a specific word only in full text indexed fields ?
I know I could just do "where field='word'" but I would rather have the search form as generic as possible and throw the search term to the CONTAINS, FREETEXT functions.
Seems there should be a word boundary or end of phrase character that could be used but there doesn't seem to be one.
I'm using MS SQL Server 2005.
In order to use Contains or FreeText, you must have a full-text index on the column(s) in question. Assuming you do, then you can force a search for a specific word by enclosing in double-quotes:
Select ..
From Table
Where Contains( SomeCol, '"word"')
CONTAINS (Transact-SQL)
Update
If what you seek is to return results where the contents is only and exactly your search term, then the only way to do that is to use = or Like without a wildcard. I.e., there is no means to search on an exact match of the entire contents using the full-text search predicates. Thus, you must use:
Select ..
From Table
Where SomeCol = 'word'
Select ..
From Table
Where SomeCol Like 'word'