I need to write a hive query in which the filter would be on the text of a field. Need to do something like where text_field contains ("pattern1" and "pattern2") or ("pattern3" and "pattern4")
I understand that we can use "like" multiple times, but I was hoping for a better alternative.
How else can it be done in Hive?
You can use rlike
TRUE if any (possibly empty) substring of A matches the Java regular expression B, otherwise FALSE. For example, 'foobar' RLIKE 'foo' evaluates to TRUE and so does 'foobar' RLIKE '^f.*r$'.
WHERE (mycolumn rlike 'pattern1' AND mycolumn rlike 'pattern2')
OR (mycolumn rlike 'pattern3' AND mycolumn rlike 'pattern4')
Related
I want to use numerical greater than operator in like clause in hive sql.
select col1,col2
from table_1
where NOT (col_3 RLIKE 'p=2')
instead of 'p=2' i want to eliminate all the values where p>2
How can i achieve that?
Any leads would be appreciated.
TIA
I am running this query in snowflake:
select *
from my_database.information_schema.tables
where
table_schema NOT LIKE '%information%';
When I look at the records, some of them have INFORMATION_SCHEMA as the table_schema.
Why is my filter not working?
LIKE is case sensitive, where-as ILIKE is case insensitive. And your two strings are different cases. So I suggest you swap to ILIKE
SELECT 'a' LIKE 'A' as "a_like_A", 'a' ILIKE 'A' as "a_ilike_A";
gives:
a_like_A a_ilike_A
FALSE TRUE
When I look at the records, some of them have INFORMATION_SCHEMA as the table_schema.
Identifiers
When an identifier is unquoted, it is stored and resolved in uppercase.
The issue is you are comparing uppercase vs lowercase string which are different.
where table_schema LIKE '%information%'; -- this comparison will not work
Other way to compare:
where table_schema LIKE UPPER('%information%');
It is worth noting that SHOW TABLES LIKE '<patern>' is case-insensitive by design, and will return a match for the following regardless of version used:
SHOW TABLES LIKE '%information%';
SHOW TABLES LIKE '%INFORMATION%';
SHOW TABLES LIKE '%Information%';
I have following table
When I run following query, it should have shown all instructor name containing 's' but it doesnot.
The query I've written:
SQL> SELECT instructor_name
2 FROM instructor
3 WHERE instructor_name LIKE '%s%';
The result is:
What is the problem here? Should not Balbir Silakar and Saurav Pangeni too must appear on the result?
's' and 'S' are two different things if your column has a case-sensitive collation.
Alas, Oracle does not provide a case-insensitive version of like (usually called ilike in other databases).
You could do:
where instructor_name like '%s%' or instructor_name like '%S%'
Or:
where lower(instructor_name) like '%s%'
Or, you can use regexp_like(); it takes a third argument that can be used to make the search case insensitive.
where regexp_like(instructor_name, 's', 'i')
I would not be surprised that the regex would be the fastest option out of the three.
As others have mentioned, s (chr(115)) and S (chr(83)) are two different things, and there is no s in 'Balbir Silakar' or 'Saurav Pangeni'.
From Oracle 12.2 you can use
where instructor_name collate binary_ci like '%s%';
You can additionally ignore accents with
where instructor_name collate binary_ai like '%s%';
Your database (by default I think) is case-sensitive, thus the LIKE matching mechanism is case-sensitive. Those names contain an upper-case S.
Here's a Q&A on how to make the search not case-sensitive. Perform a Case insensitive Like query in a case sensitive SQL Server database
For case insensitive check use regexp_like
where regexp_like(instructor_name , 's', 'i');
For a while I thought, in order for the WHERE criteria to be evaluated correctly, I need to account for case sensitivity. I would use UPPER() and LOWER() when case didn't matter. However, I am finding the below queries produce the same result.
SELECT * FROM ATable WHERE UPPER(part) = 'SOMEPARTNAME'
SELECT * FROM ATable WHERE part = 'SOMEPARTNAME'
SELECT * FROM ATable WHERE part = 'somepartname'
SQL Case Sensitive String Compare explains to use case-sensitive collations. Is this the only way to force case sensitivity? Also, if you had a case-insensitive collation when would UPPER() and LOWER() be necessary?
Thanks for help.
The common SQL Server default of a case-insensitive collation means that UPPER() and LOWER() are not required when comparing strings.
In fact an expression such as
SELECT * FROM Table WHERE UPPER(part) = 'SOMEPARTNAME'
is also non-sargable i.e won't use available indexes, due to the function applied to the part column on the left hand side of the comparison.
this query below produces CASE SENSITIVE search:
SELECT Column1
FROM Table1
WHERE Column1 COLLATE Latin1_General_CS_AS = 'casesearch'
UPPER() and LOWER() are only functions to change the case of the letter so if you case-insensitive collation, they are only use after the SELECT Keyword:
SELECT UPPER('qwerty'), LOWER('Dog')
returns
QWERTY, dog
When working with MySQL, how can I fetch all rows where the name column is all uppercase?
Since equality is case insensitive, I'm not quite sure how to do this.
If your column collation is case insensitive, you can override it in your query:
SELECT * FROM my_table WHERE my_column COLLATE latin1_bin = UPPER(my_column);
COLLATE clause syntax.
SELECT * FROM my_table REGEXP '^[[:upper:]]+$';
SELECT * FROM table where binary your_field REGEXP '^[[:upper:]]+$'
Similarly:
SELECT * FROM table where binary your_field REGEXP '^[[:upper:]]+$'
The 'binary' casts the field to binary which is necessary for REGEXP to be case-sensitive with most data types (except binary, of course).
[:character_class:] notation is documented here - there are several other useful character classes.
'binary' operator is documented here.