How to use LIKE in a query to find multiple words? - sql

I have a cust table
id name class mark
1 John Deo Matt Four 75
2 Max Ruin Three 85
3 Arnold Three 55
4 Krish Star HN Four 60
5 John Mike Four 60
6 Alex John Four 55
I would like to search for a customer which might be given as John Matt without the deo string. How to use a LIKE condition for this?
SELECT * FROM cust WHERE name LIKE '%John Matt%'
The result should fetch the row 1.
what if the search string is Matt Deo or john
The above can't be implemented when trying to find an exact name. How can I make the LIKE query to fetch the customer even if 2 strings are given?

If the pattern to be matched is
string1<space>anything<space>string2
you can write:
like string1||' % '||string2

Why not this
select * from cust where name Like 'John%Matt' ;

SELECT *
FROM custtable
WHERE upper(NAME) LIKE '%' || upper(:first_word) || '%'
AND upper(NAME) LIKE '%' || upper(:second_word) || '%'

Must you use LIKE? Oracle has plenty of more powerful search options.
http://docs.oracle.com/cd/B19306_01/server.102/b14220/content.htm#sthref2643
I'd look at those.

I believe you need REGEXP_LIKE( ):
SQL> with tbl(name) as (
select 'John Deo Matt' from dual
)
select name
from tbl
where regexp_like(name, 'matt|deo', 'i');
NAME
-------------
John Deo Matt
SQL>
Here the regex string specifies name contains 'matt' OR 'deo' and the 'i' means its case-insensitive. The order of the names does not matter.

Related

BigQuery - concatenate array of strings for each row with `null`s

This is a clarification/follow-up on the earlier question where I didn't specify the requirement for null values.
Given this input:
Row id app_date inventor.name inventor.country
1 id_1 01-15-2022 Steve US
Ashley US
2 id_2 03-16-2011 Pete US
<null> US
Mary FR
I need to extract name from inventor struct and concatenate them for each id, like so:
Row id app_date inventors
1 id_1 01-15-2022 Steve, Ashley
2 id_2 03-16-2011 Pete, ^, Mary
Note custom filling for null value - which, to me, seems like it means I need to use ARRAY_TO_STRING specifically that supports this.
The closest example I found doesn't work with nulls. How can one do this?
Use below
SELECT * EXCEPT(inventor),
(SELECT STRING_AGG(IFNULL(name, '^'), ', ') FROM t.inventor) inventors
FROM sample t
with output

Data field - search and write value in new data field (Oracle)

Sorry, I don't know how to describe that as a title.
With a query (example: Select SELECT PKEY, TRUNC (CREATEDFORMAT), STATISTICS FROM BUSINESS_DATA WHERE STATISTICS LIKE '% business_%'), I can display all data that contains the value "business_xxxxxx".
For example, the data field can have the following content: c01_ad; concierge_beendet; business_start; or also skill_my; pre_initial_markt; business_request; topIntMaster; concierge_start; c01_start;
Is it now possible in a temp-only output the corresponding value in another column?
So the output looks like this, for example?
PKEY | TRUNC(CREATEDFORMAT) | NEW_STATISTICS
1 | 13.06.2020 | business_start
2 | 14.06.2020 | business_request
That means removing everything that does not start with business_xxx? Is this possible in an SQL query? RegEx would not be the right one, I think.
I think you want:
select
pkey,
trunc(createdformat) createddate,
regexp_substr(statistics, 'business_\S*') new_statistics
from business_data
where statistics like '% business_%'
You can also use the following regexp_substr:
SQL> select regexp_substr(str,'business_[^;]+') as result
2 from
3 --sample data
4 (select 'skill_my; pre_initial_markt; business_request; topIntMaster; concierge_start; c01_start;' as str from dual
5 union all
6 select 'c01_ad; concierge_beendet; business_start;' from dual);
RESULT
--------------------------------------------------------------------------------
business_request
business_start
SQL>

How to limit returned rows using max characters in sqlite query

Say I have students table with one column named name as follows:
| name |
--------
Jhon
Natalie Kocher
Jonell Dickson
Irvin
Kiara Audet
Shawna Duvall
Cobey Maryellen
Kenny
Lindsy Taylor
How to get all rows that under 6 characters so that I get the following name:
Jhon
Irvin
Kenny
In case that's not possible at least how to sort / set the order of returned rows start from smallest length of chars to the longest.
Thanks,
About SQLite query
Use the length() function:
select t.*
from t
where length(name) < 6;
Or you can use not like:
where name not like '______%' -- there are 6 underscores

Hive UDF to return multiple colunm output

How create a UDF which take a String return multiple Strings ?
The UDF so far I have seen could only give one output. How to get multiple feilds as output from a UDF ?
Simplest would be implementation of name -> FirstName, LastName.
Not looking for alternate solution to split names, but looking for API / UDF which would help implement such needs .
Lets Say nameSplitteris my UDF
Select age,nameSplitter(name) as firstName,LastName from myTable;
InPut
****Input****
------------------------
Age | Name
------------------------
24 | John Smit
13 | Sheldon Cooper
-------------------------
OutPut
****Out put ****
-----------------------------------
Age | First Name | Last Name
-----------------------------------
24 | John | Smit
13 | Sheldon | Cooper
-----------------------------------
Use split() function, it splits strinng around regexp pattern and returns an array:
select age,
NameSplitted[0] as FirstName,
NameSplitted[1] as LastName
from
(
select age,
split(Name,' +') as NameSplitted
from myTable
)s;
Or just select age, split(Name,' +')[0] FirstName, split(Name,' +')[0] LastName from myTable;
Pattern ' +' means one or more spaces.
Also if you have three words names or even longer and you want to split only first word as a name and everything else as last name, or using more complex rule, you can use regexp_extract function like in this example:
hive> select regexp_extract('Johannes Chrysostomus Wolfgangus Theophilus Mozart', '^(.*?)(?: +)(.*)$', 1);
OK
Johannes
Time taken: 1.144 seconds, Fetched: 1 row(s)
hive> select regexp_extract('Johannes Chrysostomus Wolfgangus Theophilus Mozart', '^(.*?)(?: +)(.*)$', 2);
OK
Chrysostomus Wolfgangus Theophilus Mozart
Time taken: 0.692 seconds, Fetched: 1 row(s)
Pattern here means: the beginning of the string '^', first capturing group consisting of any number of characters (.*?), non-capturing group consisting of any number of spaces (?: +), last capturing group consisting of any number of characters greedy (.*), and $ means the end of the string

SQL - compare part of word in WHERE clausule

I have problem with sql query.
For example, I have table like this:
ID Name1 Name2 Country
1 Greg Torr Poland
2 John Smith England
3 Tom Jerry USA
I want get all record, which have for example, "la" in Country. In this case:
PoLAnd
EngLAnd
How I can put this in Where clausule?
Greets
Use the LIKE keyword:
SELECT * FROM table
WHERE Country LIKE '%la%'
I think you can use the Like Clause here. The examples are available on
http://www.sql-tutorial.net/SQL-LIKE.asp
http://www.w3schools.com/sql/sql_like.asp
etc.
WHERE CHARINDEX('LA', Country) > 0
alternatively
WHERE Country LIKE '%la%'
If your RDBMS is case-sensitive, convert Country to upper case using the appropriate string function, and compare against the upper case LA with a LIKE:
SELECT *
FROM tbl
WHERE UPPER(Country) LIKE '%LA%'
Enclose your keyword before and after with Percent Symbol
SELECT *
FROM tableName
WHERE Country LIKE '%LA%'