Exact String match using oracle CONTAINS for multiple values - sql

I need to get exact name string match from multiple values using Contains function.
I used below query to get data which matches exactly JOHN SMITH OR MIKE DAVID but query is fetching all data which has JOHN SMITH, JOHN, SMITH, MIKE, DAVID, MIKE DAVID, JOHN..SMITH, JOHN/SMITH,....
where contains(names,'{JOHN SMITH} OR {MIKE DAVID}')>0
Note - I don't want to use multiple like in OR conditions.We need to pass around 200 to 300 values (names) to do match pattern.
Can anyone let me know how to get exact match from multiple values using CONTAINS?
Thanks
Anand

I don't use Oracle but i liked this question and i will try to answer it based on what i read on Oracle's documentation, regarding CONTAINS.
so i am currently reading about Stored Query Expression (SQE),
Stored Query Expression (SQE)
Use the SQE operator to call a stored query expression created with
the CTX_QUERY.STORE_SQE procedure.
Stored query expressions can be used for creating predefined bins for
organizing and categorizing documents or to perform iterative queries,
in which an initial query is refined using one or more additional
queries.
So perhaps you could pass all the names you want to query in such a way. For example:
begin
ctx_query.store_sqe('mynames', 'JOHN SMITH OR MIKE DAVID OR GEORGE HARRIS OR JOHN DOE');
end;
And then call it:
SELECT SCORE(1), nameid FROM mytable
WHERE CONTAINS(names, 'sqe(mynames)', 1)> 0
ORDER BY SCORE(1);
Hope this was helpful.

Related

Alteryx Designer - How to retrieve only first and last name from field excluding middle initials?

I need help in writing SQL code in Alteryx Designer.
My table employees contains a column Name with values shown below. However, I need the expected output as shown below.
Please help.
Name:
Smith, Mary K
Koch, J B
Batoon Rene, Anne S
Vaughan-tre Doctor, Maria S
Excepted output:
Smith, Mary
Koch, J
Batoon Rene, Anne
Vaughan-tre, Maria
The middle initials and “Doctor” word is removed.
Not sure why you need to use SQL if you have the data in Alteryx?
So, you need to remove the right hand 2 characters and the word 'Doctor' from each record?
You could use the Formula tool, though I suspect there are numerous other ways:
replace (TrimRight([Name],' '+right([Name],1)),'Doctor','')

SQL WHERE column values into capital letters

Let's say I have the following entries in my database:
Id
Name
12
John Doe
13
Mary anne
13
little joe
14
John doe
In my program I have a string variable that is always capitalized, for example:
myCapString = "JOHN DOE"
Is there a way to retrieve the rows in the table by using a WHERE on the name column with the values capitalized and then matching myCapString?
In this case the query would return two entries, one with id=12, and one with id=14
A solution is NOT to change the actual values in the table.
A general solution in Postgres would be to capitalize the Name column and then do a comparison against an all-caps string literal, e.g.
SELECT *
FROM yourTable
WHERE UPPER(Name) = 'JOHN DOE';
If you need to implement this is Knex, you will need to figure out how to uppercase a column. This might require using a raw query.

Full text search on multiple columns sql server

I have the following table
Id Author
1 Alexander Mccall Smith
2 Ernest Hemingway
3 Giacomo Leopardi
4 Henry David Thoreau
5 Mary Higgins Clark
6 Rabindranath Tagore
7 Thomas Pynchon
8 Zora Neale Hurston
9 William S. Burroughs
10 Virginia Woolf
11 William tell
I want to search the Author by putting first few characters of the first and last name.
eg: Search Text: Will tel
Then the search result show the following result
William tell
eg: Search Text: will Burrou
Then the search result show the following result
William S. Burroughs
eg: Search Text: Will
Then the search result show the following result
William S. Burroughs
William tell
What is the efficient way to achieve this in sql server ?
As you mentioned this can be achieved using Full Text Search. You have to create the FTS catalog and then index on the table and column(s). You stated in the title 'Columns' but I only see one table column in your example so I will create the queries using that.
-- example 1 searching on Will and Tel
SELECT Id, Author
FROM Authors
WHERE CONTAINS(Author, '"Will*" AND "tel*"')
-- example 2 searching on Will and Burrou
SELECT Id, Author
FROM Authors
WHERE CONTAINS(Author, '"will*" AND "Burrou*"')
-- example 3 searching on Will
SELECT Id, Author
FROM Authors
WHERE CONTAINS(Author, '"will*"')
For further reference see
The Contains clause which searches for precise or fuzzy matches.
Article Query with Full-Text Search.
Less efficient than #Igor's answer as the table size grows, but you can also use the Like statement.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
-- example 1 searching on Will and Tel
SELECT Id, Author
FROM Authors
WHERE Author Like('Will%Tel%')
-- example 2 searching on Will and Burrou
SELECT Id, Author
FROM Authors
WHERE Author Like('Will%Burrou%')
-- example 3 searching on Will
SELECT Id, Author
FROM Authors
WHERE Author Like('Will%')
Cons: It is slower than the contains statement.You need to include the % sign after any other keyword you're looking to search for.
Pros: Can be faster than contains statement in cases of smaller(<1000) row tables.

Nonexact duplication, sum rows in pivot table

I have a spreadsheet for payroll that is populated from a seperate spreadsheet. Occasionally,one of our workers will get a promotion. That promotion shows on the timesheets: ex. Smith, Adam Position becomes Smith, Adam Promotion.
This data is then populated into a pivot table where Smith, Adam Position and Smith, Adam Promotion show in separate cells. Currently, we are manually adding the two data sets so that payroll gets a single number instead of multiple. I would like to simplify this tasks. I am using excel 2003, so some more advanced functions don't work.
Any suggestions and help would be greatly appreciated. Thanks in advance.
Ideally, you'd use a different field (a unique identifier) to identify Smith, Adam (e.g., an employee ID number), but if that's not available, then you could take the following approach:
(Suppose that "Smith, Adam Position" is in A1.)
You could add an additional column that extracts the last name, the comma, and then whatever the next word is. For example, from
Smith, Adam Analyst
you would get Smith, Adam. Unfortunately, this means that If you have something like
Jones, Mary Ellen Consultant
you would end up with Jones, Mary. If you think you can live with that, this solution could work. The way you would extract that would be with the following formula:
=SUBSTITUTE(LEFT(SUBSTITUTE(A1,", ",",",1),FIND(" ",A1)-1),",",", ",1)
And then build your pivot table on that field.

How do you query only part of the data in the row of a column - Microsoft SQL Server

I have a column called NAME, I have 2000 rows in that column that are filled with people's full names, e.g. ANN SMITH. How do I do a query that will list all the people whose first name is ANN? There are about 20 different names whose first name is ANN but the surname is different.
I tried
and (NAME = 'ANN')
but it returned zero results.
I have to enter the FULL name and (NAME = 'ANN SMITH') ANN SMITH to even get a result .
I just want to list all the people with there first name as ANN
Try in your where clause:
Where Name like 'ANN %'
Should work mate.
ANN% will find all results where ANN is first then anything after.
%ANN% will find the 3 letters ANN in any part of that rows field.
Hope it helps
Also usually Name is separated into First names and second name columns.
this will save Having to use wild cards in your SQL and provide A bit more normalized data.
SELECT NAME
FROM NAMES
WHERE NAME LIKE 'ANN %'
This should wildcard select anything that begins with 'ANN' followed by a space.