Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-10-19
6
Medium Priority
?
59 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 1000 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 40

Assisted Solution

by:PatHartman
PatHartman earned 500 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 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 500 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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 40

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

916 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