Solved

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

Posted on 2016-10-19
6
49 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 37

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 50

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 37

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

688 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