Solved

How To Identify Records From Two Different SQL Tables

Posted on 2016-08-05
2
32 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
ID: 41744910
% 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
ID: 41744967
It worked perfectly!!  Thank you very much!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

805 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