Solved

How To Identify Records From Two Different SQL Tables

Posted on 2016-08-05
2
39 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
[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
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TSQL convert date to string 4 73
too many installs coming along with SQL 2016? 1 36
Need to trim my database size 9 53
SQL Server replace between tags 6 31
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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