Hive LIKE query returns some like results, several unlike results, and some NULL results - hive

When I queried from hive with 'LIKE', it looks like
select * from table where a_column like '%http%';
It returns me some results where a_column does not contain http and some NULL results.
I want to know whether my query sentence is serious or not.
Is there any pattern more seriously?

Please use in the below way using concat_ws.
select * from table where concat_ws(',',map_values(a_column)) like '%http%';

Related

Select name from system table and select from this table

I need dynamically obtain table name from system table and perform a select query on this table example:
SELECT "schema"+'.'+"table" FROM SVV_TABLE_INFO WHERE "table" LIKE '%blabla%'
it returns my_schema.the_main_blabla_table
And after I get this table name I need to perform :
SELECT * FROM my_schema.the_main_blabla_table LIMIT 100
Is it possible to in a single query?
If you are talking about select subquery after "from" i can say that you can do this.
You will get something like this:
SELECT * FROM
(
SELECT "schema"+'.'+"table" FROM SVV_TABLE_INFO WHERE "table" LIKE '%blabla%'
)
LIMIT 100
Unfortunately, i can't test it on yor data, but i very interested in result because i have never done something like this. If i get your question incorrect, tell me pls.
Amazon Redshift does not support the ability to take the output of a query and use it as part of another query.
Your application will need to query Redshift to obtain the relevant table name(s), then make another call to Redshift to query that table.

Why do CONTAINS and LIKE return different results?

I have the following query. There are two possible columns that may hold the value I'm looking for, let's call them FieldA and FieldB.
If I execute this:
SELECT COUNT(1)
FROM Table
WHERE CONTAINS(Table.*, 'string')
I get back "0".
However, if I execute this:
SELECT COUNT(1)
FROM TABLE
WHERE FieldA LIKE '%string%' OR FieldB LIKE '%string%'
I get back something like 9000. I then checked and there are rows that have the word string in either FieldA.
Why does this happen? I recall that CONTAINS uses a full-text index, but I also recall that LIKE does the same, so if the problem was that the indexes are outdated, then it should fail for both of them, right?
Thanks
I believe that CONTAINS and full text searching will only yield whole word results, so you won't match the same as LIKE '%string%'. If you want to right wildcard your CONTAINS, you must write it like:
SELECT COUNT(1) FROM Table WHERE CONTAINS(Table.*, '"string*"')
However, if you want to left wildcard, you can't! You have to store a copy of your database reversed and then do:
SELECT COUNT(1) FROM Table WHERE CONTAINS(Table.*, '"gnirts*"')
https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ms552152(v=office.14)
How do you get leading wildcard full-text searches to work in SQL Server?
So in the example in the question, doing a CONTAINS(Table.*, 'string') is not the same as doing LIKE '%string%' and would not have the same results.

Oracle DB LIKE query on NULL values

This question is about Oracle DB. I want to know how Oracle DB query LIKE clause works on NULL values.
Select * From table_name where column_name like 'text%'
In above query, how the Oracle db treat to rows with null value for column 'column_name'? Also how about 'NOT LIKE'.
Also, I observed that rows having NULL values are not selected for the following query.
Select * From table_name where column_name NOT LIKE 'text%' .
I don't know why rows having NULL values for the column are not in results even though they are null and hence not like 'text%' .
NULL values basically fail all comparisons. The general idea is that NULL means "I don't know what the value is". So, when you use like with the pattern 'text%', the answer is "I don't know what the value is". It is NULL.
And if you use not like, the answer is the same "I don't know what the result is".
That is how NULLs work. Even with like and not like. Even with Oracle.
First one,
When you search for column_name like 'text%', db search for string that starts with "text" not other string, it doesn't matter what will come after the text. It could be anything like text123,text stack etc.
Second one,
When you search for NOT LIKE 'text%', db search for all the columns that should not be started with text, it the column value have text it will not be in the result. it is like "atext", it will be appear in the search results.
So in both condition NULL values never match so they don't come in the results.
Hope it will help.
Try this,
SELECT *
FROM table_name
WHERE NVL(column_name,1) NOT LIKE NVL('',2) -- '' OR NULL you can use
Try this,
SELECT *
FROM table_name
WHERE NVL(column_name,1) NOT LIKE NVL('',2) -- '' OR NULL you can use

Fastest way to check if any case of a pattern exist in a column using SQL

I am trying to write code that allows me to check if there are any cases of a particular pattern inside a table.
The way I am currently doing is with something like
select count(*)
from database.table
where column like (some pattern)
and seeing if the count is greater than 0.
I am curious to see if there is any way I can speed up this process as this type of pattern finding happens in a loop in my query and all I need to know is if there is even one such case rather than the total number of cases.
Any suggestions will be appreciated.
EDIT: I am running this inside a Teradata stored procedure for the purpose of data quality validation.
Using EXISTS will be faster if you don't actually need to know how many matches there are. Something like this would work:
IF EXISTS (
SELECT *
FROM bigTbl
WHERE label LIKE '%test%'
)
SELECT 'match'
ELSE
SELECT 'no match'
This is faster because once it finds a single match it can return a result.
If you don't need the actual count, the most efficient way in Teradata will use EXISTS:
select 1
where exists
( select *
from database.table
where column like (some pattern)
)
This will return an empty result set if the pattern doesn't exist.
In terms of performance, a better approach is to:
select the result set based on your pattern;
limit the result set's size to 1.
Check whether a result was returned.
Doing this prevents the database engine from having to do a full table scan, and the query will return as soon as the first matching record is encountered.
The actual query depends on the database you're using. In MySQL, it would look something like:
SELECT id FROM database.table WHERE column LIKE '%some pattern%' LIMIT 1;
In Oracle it would look like this:
SELECT id FROM database.table WHERE column LIKE '%some pattern%' AND ROWNUM = 1;

MySQL question about "reverse LIKEs"

Well given I have a value I want to check for potential matches in a database (in one varchar field) so I write something like:
SELECT * FROM table WHERE column LIKE "%value%"
Which will work if the value is something like "test" and the column has a value of "this is a test" however if it is reversed then I will not get a match I have tried things along the lines of:
SELECT * FROM table WHERE CONCAT("%",column,"%") LIKE "value"
but don't know exactly how to phrase this to Google to get a response I need, please help!
You can reverse a like statement. Just using the same syntax as a regular like query:
select
*
from
table
where
'value' like concat('%', column, '%')
Of course, if you felt wild and crazy, you could also use instr:
select * from table where instr('value', column) > 0
I don't know which one is faster, since I don't have a MySQL instance to test against, but it's worth trying both to see which wins.
SELECT *
FROM table
WHERE 'value' LIKE CONCAT('%', column, '%')