Solved

How To Identify Records From Two Different SQL Tables

Posted on 2016-08-05
2
24 Views
Last Modified: 2016-08-05
I need to identify records (BY THEIR FIRST SIX (6)) digits between two tables.  I using the below SQL statement:

SELECT TABLE2.COMPANY_ID AS ACCOUNTS_WITH_A_STATUS, TABLE1.ACCOUNT AS MATCHING_ACCOUNTS
FROM TABLE1
JOIN TABLE2 ON TABLE1.ACCOUNT = TABLE1.ACCOUNT
WHERE TABLE2.STATUS = 'A' AND TABLE2.COMPANY_ID + '%%%%%%' LIKE '%%%%%%' + TABLE1.ACCOUNT + '%%%%%%'

At first, this query seemed to work, however, it seems to be matching the LAST six digits, instead of the FIRST six digits.  Please see attachment.  Any assistance with this issue would be greatly appreciated.
Sample_1.PNG
0
Comment
Question by:Member_2_7970395
2 Comments
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
Comment Utility
% is the joker any number of any characters. You want to use _ for any single character or [0-9] for a digit, so the LIKE pattern for 6 digits is '[0-9][0-9][0-9][0-9][0-9][0-9]'.

If you want the first 6 character to match, keep it simple, use LEFT(table1.column,6) = LEFT(table2.column,6).

col LIKE '[0-9][0-9][0-9][0-9][0-9][0-9]' will match any 6 digit number, but you want them to be same, not any number, don't you?

Bye, Olaf.
0
 

Author Comment

by:Member_2_7970395
Comment Utility
It worked perfectly!!  Thank you very much!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

9 Experts available now in Live!

Get 1:1 Help Now