Solved

joining two tables on primary key but with just first 10 characters

Posted on 2016-10-19
6
23 Views
Last Modified: 2016-10-20
I had this question after viewing disregard leading zero in query.


Access 2010 vba

I have 2 tables:
table1 primary key    MyKey1
SLS-002213-MAIN-A1   is an example value

table2 primary key    MyKey2
SLS-002213   is an example value

So in order to get the join to work:
I just need the text in MyKey1 to be just 10 characters to match  MyKey2 primary key.



Thanks
fordraiders
0
Comment
Question by:fordraiders
6 Comments
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 250 total points
ID: 41850867
I'd use this in the SQL
SELECT t1.*
FROM Table1 t1 INNER JOIN Table2 t2 ON
      LEFT(t2.MyKey2,10) = LEFT(t1.MyKey1,10)

Kelvin
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 125 total points
ID: 41850932
The best solution is to not mush multiple attributes into a single field.  Each column of a table is supposed to be "atomic".  That means that it cannot be further subdivided and still have meaning.  Storing concatenated data in a single field violates first normal form.  

If you haven't gone too far with this design, I would suggest breaking the "key" into it's constituent parts.  Then I would add an autonumber and make it the primary key.  Then I would create a unique index on all the parts of the old PK so you can enforce the business rule of uniqueness.  All child tables would replace the existing FK with an FK that references the new autonumber PK.

However, it is possible that you need to go even further in your normalization process and that would be to create a new table.  The new table would represent the high level part of the old PK and any attributes that relate to it specifically would move with it to the new parent table.  The remaining columns would now be a child table.
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 125 total points
ID: 41851071
You can use this simple SQL which is viewable in the designer:

SELECT 
    Table1.*,
    Table2.*
FROM 
   Table1,
   Table2
WHERE
   LEFT(Table1.MyKey1,10) = Table2.MyKey2;

Open in new window


/gustav
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Author Comment

by:fordraiders
ID: 41852624
pat, always enjoy the comments. Unfortunately I inherit things i cant change.
But I agree.

dp
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 41852626
thanks all
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 41852635
Sometimes you can do a restructure with minimal disruption.  You would have to rename the tables and then create a query that joins them back so they look like the original table and you just give the new query the name of the original table.  It's a bit hokey but it might end up saving a lot of effort in the long run.

Anyway, I understand the limitations for existing apps so good luck.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now