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:

It works but it does not link the table B.

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 HornSQL Server Data DudeCommented:
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
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.
Michael DyerSenior Systems Support AnalystCommented:
You could try something like this:


It inserts the dash in the A value after the 4th character just for the join
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

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.
Jim HornSQL Server Data DudeCommented:
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.
ForeroJAuthor Commented:
Jim HornSQL Server Data DudeCommented:
Thanks for the .pdf image, but next time please do this -->  Custom Image from a Screenshot
Jim HornSQL Server Data DudeCommented:
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.
ForeroJAuthor Commented:
It is sql statement from visual dataflex programing tool accessing sql server tables.
Vitor MontalvãoMSSQL Senior EngineerCommented:
I can't see any issue from the PDF file.
Looks like is returning rows.
PortletPaulEE Topic AdvisorCommented:
taken from pdf attachmentREPLACE() of the hyphen with blank should indeed produce equality:
    , 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?

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.