How to join 2 tables in a SQL statement on a field with different value format.

I have 2 tables: A and B.
Both tables have a field named ITEM.
The ITEM format in table A is like: ABCD-ABC12345678
The ITEM format in table B is : ABCDABC12345678 (with no "-")
How can in join this 2 tables based on the ITEM field?

I have tried this way:
SELECT A.ITEM, B.VENDNUM
FROM A LEFT JOIN B ON (REPLACE(A.ITEM,'-','')) = B.ITEM

It works but it does not link the table B.


Thanks,
Jose.
ForeroJAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Define 'It works but it does not link the table B.' in more detail, preferably with some sample data.
Also tell us what the column data types are for A.ITEM and B.ITEM
0
ForeroJAuthor Commented:
The result of the statement list the records in table A but not the correspondent records in table B.
The field type is string.
0
Michael DyerSenior Systems Support AnalystCommented:
You could try something like this:

SELECT A.ITEM, B.VENDNUM
 FROM A LEFT JOIN B ON (SUBSTRINGA.ITEM+ '-' + SUBSTRING(A.ITEM,5,40)) = B.ITEM

It inserts the dash in the A value after the 4th character just for the join
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Scott PletcherSenior DBACommented:
Something else must be going on.  After the REPLACE, those values would join just fine if they were equal, so something else is making them not equal.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Without some sample data we can only guess what's going on, as the REPLACE statement seems fine.

So spill .. show us some sample data.

And string is not a field type in SQL Server.  char, varchar are.
0
ForeroJAuthor Commented:
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the .pdf image, but next time please do this -->  Custom Image from a Screenshot
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Couple of thoughts..
Are you querying this in Access or SQL Server?
There may be some leading or trailing spaces surrounding these values which are causing the JOIN to fail, which can be resolved using LTRIM(RTRIM(the value)) in SQL Server or Trim(the value) in Access.
0
ForeroJAuthor Commented:
It is sql statement from visual dataflex programing tool accessing sql server tables.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
I can't see any issue from the PDF file.
Looks like is returning rows.
0
PortletPaulfreelancerCommented:
taken from pdf attachmentREPLACE() of the hyphen with blank should indeed produce equality:
select
      test
    , item1
    , item2
    , case when item1 = item2 then 'equal' else 'not equal' end
from (
      select 'raw' as test
          , 'ACADAC_2601BK BU' as item1
          , 'ACAD-AC_2601BK BU' as item2
      union all
      select 'replaced' as test
          , 'ACADAC_2601BK BU' as item1
          , REPLACE('ACAD-AC_2601BK BU','-','') as item2
      ) d
;

|     test |            item1 |             item2 |           |
|----------|------------------|-------------------|-----------|
|      raw | ACADAC_2601BK BU | ACAD-AC_2601BK BU | not equal |
| replaced | ACADAC_2601BK BU |  ACADAC_2601BK BU |     equal |

Open in new window

what else might there be in those strings to make them different?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.