Access Query for 2 tables with similar data - sql

I have two tables that have a column labeled Name:
[CurrentRecords].[Name]
[tbl_vPC].[Name]
However, the CurrentRecords table has more info on each name such as jr, sr, II, III, etc, but the tbl_vPC does not contain that extra information.
Example:
CurrentRecords has: ROBINSON, ROBERT E JR
tbl_vPC has: ROBINSON, ROBERT E
CurrentRecords has: ALLEN, DUG V III
tbl_vPC has: ALLEN, DUG V
Is there a query where I can find all records in the CurrentRecords and all records in tbl_vPC that are similar?

Name is a reserved word. Should not use reserved words as names.
This is why name parts should be in separate fields. Parsing first and last names might not be terribly difficult because of the comma, assuming EVERY record has this convention and EVERY record has value in this field. Try in queries for each table:
x represents Name field
LastName: Left(x, InStr(x, ",")-1)
FirstName: Left(Mid(x, InStr(x,",")+2), InStr(Mid(x, InStr(x,",")+2)," "))

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.

CASE ReGex with substring

I'm writing a SQL query where I am taking the substring of 2 names (First name/last name) to create an initials column, the data is unstructured to a certain extent (Can't show for GDPR reasons) but where there is a company name it is just in the surname column.
I'm trying to use Regex to say when the already present initials column is 1 letter (I.e not an initial) and if it is not an initial run a command that I wrote which successfully works.
CAST(CASE
WHEN [DATA_TABLE].[INITIALS] = '\d' THEN (CONCAT(substring([DATA_TABLE].[FIRSTNAMES],1,1),substring([DATA_TABLE].[SURNAME],1,1)) AS char) AS INITIALS
ELSE [DATA_TABLE].[INITIALS]
end as char) as INITIALS,
An example of the data format:
First name last name initials
John smith JS
Electrical company E
Sam Craig SC
I want the names that are just in the surname (Company names) to just remain as they are with no change (I.e The \d regex). Ones which don't will become the substring of their first name as (1,1) and a substring of their last name to also be (1,1).

SQL get student with last name 5 characters

I have a table called Students.
This table has two fields (ID, Name)
i need to Select all the students whose last name have 5 characters.
For example if i have in this table two records.
Student 1: ID - 1
Name - Roman Jatt Pearce
Student 2: ID:2
Name: Matt Crazy
The query i wanted should only return Matt Crazy since his last name has 5 characters and roman pearce's doesnt.
someone told me to use charindex but i dont really know how to implement it
any suggestion?
Assuming the format of Name is always "First Middle Last", no names contain spaces, and there are no other things like generation listed (Jr., Sr., et al).
SELECT *
FROM Students
WHERE CHARINDEX(' ', REVERSE(Name)) = 6
How about
select * from Students where Name like '% _____'
with dash symbol coming five times

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.