Oracle SQL: how to search honorofics in a string - sql

I have name fields in my data set. Using Oracle PL SQL, how can I search for the records that contain honorifics?
I have a list of honorifics that I want to search for in a separate table.
Any help would be really appreciated.
Thanks.

I'd use REGEXP_LIKE and do a cross join against the honorifics table.
This query will list all names that have an honorific, plus the honorific. If a name has more than one honorific it will be listed for each match:
SELECT
myTable.Name,
honorifics.Title
FROM myTable
CROSS JOIN honorifics
WHERE REGEXP_LIKE(myTable.Name, ''(\W|^)' || honorifics.Title || '(\W|$)')
The regex checks to see if the honorific title is at the beginning of the string or preceded by a "non-word" character, and if it's at the end of the string or followed by a non-word character.
Note that this search is case sensitive. To make it non-case sensitive, add a third argument of 'i' to the REGEXP_LIKE:
WHERE REGEXP_LIKE(myTable.Name, ''(\W|^)' || honorifics.Title || '(\W|$)', 'i')
^^^^^

SELECT names.lastName
FROM names
INNER JOIN honorophics ON names.lastName LIKE honorophics.listOfHonorophics + '%'
this will join the 2 tables, resulting in the rows, where lastName contains a pattern from the table with honorophics

Related

substring match on both tables of a join

How can I perform joins on a substring match to another substring. I seem to only be able to ilike search on one or the other, not substring search both.
Given tables:
DIALOG
string
-------------------
Hi, my name is dan
STRUCTURES
structure
----------
his name is / my name is
hello, my / you are
how are you?
EXPECTED OUTPUT:
string | structure
-------------------------------
Hi, my name is dan | his name is / my name is
Attempts:
Two ilike fuzzy matches:
select string, structure from dialog left join structures on ('%' || string || '%' ilike '%' || structure || '%');
Two fuzzy ilike matches with OR:
select string, structure from dialog left join structures on (string ilike '%' || structure || '%') or (structure ilike '%' || string || '%');
Both output:
string | structure
-------------------------------
Hi, my name is dan |
If the structures actually matches, you could use regular expressions:
select string, structure
from dialog d left join
structures s
on string ~ replace(string, ' / ', '|');
Of course, this doesn't work on the sample data, because the strings don't actually match.
This also suggests that your structure should actually be a regular expression.
Perform a cartesian product first, limited with a WHERE clause, to see what kind of results you can expect.
select string, structure from dialog CROSS join structures WHERE string ilike '%' || structure || '%' AND structure ilike '%' || string || '%'
I think your left join attempt does not match anything because there's wildcards on the left side of the ILIKE statement. These are, afaik, taken literally. Also, use 'AND' for the join: you want the couples where both of the predicates are true. The cross join fits OK here, as you define your where clause pretty tightly.
The left join would only be used where you want to absolutely get your 'dialog', with optionally 'structure' connected to it. In this case, do the 'full join', so you can see exactly what kind of matches are made. Later on, you can decide to further filter everything out and put the where clause predicates in suitable join clause.

"NOT IN" subquery with a leading wildcard

I have two tables:
Table tablefoo contains a column fulldata.
Table tablebar contains a column partialdata.
I want find a list of tablefoo.fulldata that do NOT have partial matches in tablebar.partialdata.
The following provides a list of tablefoo.fulldata with partial matches in tablebar, but I want the negative of this.
select fulldata from tablefoo
where fulldata like any (select '%' || partialdata from tablebar);
This lists every record in partialdata:
select fulldata from tablefoow
where partialdata not in (select '%' || partialdata from tablebar);
Any idea how to get only the results tablefoo.fulldata that do not contain matches to a leading wildcarded tablebar.partialdata?
I found this link: PostgreSQL 'NOT IN' and subquery which seems like it's headed down the right path, but I'm not getting it to work with the wildcard.
Sure, I could write a script to pull this out of psql and do the comparisons, but it would be much nicer to handle this all as part of the query.
SELECT fulldata
FROM tablefoo f
WHERE NOT EXISTS (
SELECT 1
FROM tablebar b
WHERE f.fulldata LIKE ('%' || b.partialdata)
);

Use string contains function in oracle SQL query

I'm using an Oracle database and I want to know how can I find rows in a varchar type column where the values of that column has a string which contains some character.
I'm trying something like this (that's a simple example of what I want), but it doesn't work:
select p.name
from person p
where p.name contains the character 'A';
I also want to know if I can use a function like chr(1234) where 1234 is an ASCII code instead of the 'A' character in my example query, because in my case I want to search in my database values where the name of a person contains the character with 8211 as ASCII code.
With the query select CHR(8211) from dual; I get the special character that I want.
Example:
select p.name
from person p
where p.name contains the character chr(8211);
By lines I assume you mean rows in the table person. What you're looking for is:
select p.name
from person p
where p.name LIKE '%A%'; --contains the character 'A'
The above is case sensitive. For a case insensitive search, you can do:
select p.name
from person p
where UPPER(p.name) LIKE '%A%'; --contains the character 'A' or 'a'
For the special character, you can do:
select p.name
from person p
where p.name LIKE '%'||chr(8211)||'%'; --contains the character chr(8211)
The LIKE operator matches a pattern. The syntax of this command is described in detail in the Oracle documentation. You will mostly use the % sign as it means match zero or more characters.
The answer of ADTC works fine, but I've find another solution, so I post it here if someone wants something different.
I think ADTC's solution is better, but mine's also works.
Here is the other solution I found
select p.name
from person p
where instr(p.name,chr(8211)) > 0; --contains the character chr(8211)
--at least 1 time
Thank you.
You used the keyword CONTAINS in your sample queries and question. CONTAINS lets you search against columns that have been indexed with an Oracle*Text full-text index.
Because these columns are full-text indexed, you can efficiently query them to search for words and phrases anywhere with the text columns without triggering a full table scan. Depending upon their usage, using LIKE or INSTR will almost always result in a full table scan.
CONTAINS is used to search for words and phrases. Although there are many options it is not appropriate if you are looking for embedded characters such as 'A' or chr(8211).
The following query will return all rows that contain the word "smith" anywhere in their text.
SELECT score(1), p.name
FROM person p
WHERE CONTAINS(p.name, 'smith', 1) > 0;
For more details see:
How does contains() in PL-SQL work?
Oracle SQL "contains" clause tips
Oracle: Contains Documentation
Oracle: Contains Operators
Just in case you need you need to find if a column has any values that have character in it, you can use regexp_like. The first parameter is the column name to be checked and the second parameter is the regular expression.
If the below sql returns count greater than zero, that means there are some row(s) in which there is 1 or more character in it.
SELECT COUNT(*)
FROM TABLE_NAME
WHERE regexp_like (COLUMN_NAME, '[^0-9]')

Wildcards in sql

How does wildcards works in sql. If I do select * from table it give all the fields. But if I do select a* from table it gives error. Shouldn't it give all fields which begins with a?
I am little confused.
SELECT * FROM tableName literally means "select all columns from tableName".
Philip Graham is right about his answer where he asked to use a.*
Wildcards help you search for strings about which you are not sure. These are almost always used with the LIKE keyword and put in WHERE clauses or searched CASE statements.
There are two wildcard characters - % and _.
% is used to find any string of 0 or more length.
E.g.,
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J%'
This will return all the firstName from the persons table where firstname starts with letter J. This would return "Jason", "James", "Josh", "Jessica" and much more.
Note that UPPER function was used to eliminate case sensitivity.
Next, you can have an _ character that looks for the presence of one single character.
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J_M__'
This would return "James", "Jimmy", "Jamos", "Jxmx" and filter away any "Jason", "Jaguar", etc.
For more info click here
You can use a.* where a is the name of the table. For instance in
select a.* from a left join b on a.id = b.id
You would return only the fields from a but not from b
If want to use a wild card in SQL, You need to key on the column that you want to filter using LIKE.
SELECT *
FROM table
WHERE column_name LIKE 'a%';
This will give you everything that begins with 'a' on that column.
If you don't want all the columns, you must explicitly give the name of each column that you want in the query.
SELECT LastName, FirstName, Address
FROM table
So if you want all the fields that begin with 'a' you must name all the fields that begin with 'a' in the SELECT statement.
Hope this helps.

T-SQL Using CONTAINS predicate with Hyphen

Imagine a table (table1) with one column (column1) and one record whose value is 'roll-over'. Then use the following SQL query and you will not get any records.
select * from table1 where contains(column1, ' "roll-over" ')
Is there a way to escape the hyphen in the search text? So far I have not been successful trying this (I have tried all below escapes with no success).
select * from table1 where contains(column1, ' "roll\-over" ')
select * from table1 where contains(column1, ' "roll!-over" ')
select * from table1 where contains(column1, ' "roll[-]over" ')
Also, please note that using the LIKE keyword is not possible for my application because I am taking advantage of full-text search indexing.
It looks like you may not be able to do that. Give this article a read:
https://support.microsoft.com/en-us/help/200043/prb-dashes---ignored-in-search-with-sql-full-text-and-msidxs-queries
They suggest searching only alphanumeric values (lame) or using the LIKE Clause (not an option for you).
Partial solution: you can force the query to return records containing hyphens (or any character) by using the charindex function to test that the string contains the character, e.g.:
select * from table1 where contains(column1, ' "roll-over" ')
and charindex('-', column1) > 0