Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3671822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:32:02+00:00 2026-05-19T02:32:02+00:00

I have a question because I’m really bad at SQL. I understand basic functions

  • 0

I have a question because I’m really bad at SQL. I understand basic functions but when
it gets a bit more complex, I’m completly lost.

here is what I have:
tables: tA, tB
columns: tA: refA tB: refB
basically refA and refB represent the same thing (some id of a form like xxx-xxx-xxx), but
refB can have information appended (like xxx-xxx-xxx_Zxxx or xxx-xxx-xxx Zxxx)

here is what I know how to do:
querying items that are in a table but not in another (when they are exactly the same)

select refA
from tA
where not exists (select *
from tB
where tB.refB = tA.refA
)

What i want to do:
I want a query that will list items from refA that are not in refB.
BUT, Problem is if I run a “simple” query with a NOT EXISTS like I just showed, it will return everything,
because of the appends. so I thought about using some syntax like this:

SELECT refA
FROM tA
WHERE NOT EXISTS (SELECT * 
FROM tB
WHERE tB.refB LIKE CONCAT(tA.refA,'%'))

but… of course, it doesn’t work.

Could someone show me how it should be done, and also explain how it works, so I can learn ?
Thanks in advance !

edit: additional info
I can’t use a left() or something alike, because the ref format is similar but not always the same (varies in number of characters).
The only way to detect the end of the id before the append, is that there is either a blank space or an underscore.

edit 2: data sample causing problems (MON, Jan. 10th)

here is some actual data from the tables, which makes most answers people have given here
miss some results :/

in tA:
B20-60-04-6A-1
B20-60-04-6A-11
B20-60-04-6A-12
B20-60-04-6A-13

in tB:
B20-60-04-6A-11_XX
B20-60-04-6A-12_XX
B20-60-04-6A-13_XX

problem with mid(), left(), etc. is that if we check “B20-60-04-6A-1” (14 chars)
against the 14 first chars, it will return 3 positives, while in fact it is not in tB…

so, how can we proceed ?

Examples of data patterns in tA are like this:
(X, XYZ: charaters. x: alphanumerical)
Xxx-xx-xx-x
Xxx-xx-xx-xx
Xxx-xx-xx-xx-xx
Xxx-xx-xx-xx-xx-x
etc

examples of data patterns in tB:
Xxx-xx-xx-xx-xx-XYZ-xx Z xxx_XX
Xxx-xx-xx-xx-xx-XYZZxxx_XX
Xxx-xx-xx-xx-xx Z xxx_XX

XYZ are always the same 3 characters. When we do not have XYZ, there is always a blank space or an underscore.

so the string of data we compare should be trimmed according to this:
– from start to -XYZ string
– or, if no -XYZ in the string, from start to the first ” ” or “_”

I’d write that lightning fast in VBA, but in SQL… well, I’ll give it a shot, but I’m really bad at it 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:32:03+00:00Added an answer on May 19, 2026 at 2:32 am

    So, first off, you need a function that will change refB to not have the appended information, so it can be compared properly with refA. There will be several approaches, but something like this should work:

    Left(tb.RefB, InStr(Replace(tb.RefB+"_", " ", "_"), "_") -1)
    

    That will convert any refB like “123-456 123 EXTRA STUFF” or “123-456_123_EXTRA_STUFF” into “123-456”. That result should then be okay to compare directly with a refA.

    EDIT: A short explanation of the expression above. What I’m doing is:

    1. Adding an underscore to the end of refB, so that there’s always at least one underscore (this copes for the case where refB is the same as refA, e.g. “123” becomes “123_”)
    2. Replacing all spaces in refB with underscores (the Replace function). Now we know that the separator is always an underscore, and we also know from step 1 that there will be at least one underscore.
    3. Finding the location of the first underscore (the InStr function). This is the position where refB is split between refA and the additional stuff.
    4. Grabbing all the characters between the start of the string and this first underscore, i.e. the part before the separator.

    So, that gives you something like this:

    select refA
    from tA
    where not exists (select *
    from tB
    where Left(tb.RefB, InStr(Replace(tb.RefB+"_", " ", "_"), "_") -1) = tA.refA
    )
    

    I would use this approach rather than comparing with wildcards, or trimming refB to match the length of refA, because of this scenario:

    refA
    ====
    123
    123-456
    123-456-789
    
    refB
    ====
    123-456-789_This_is_a_test
    

    In this case, trimming or wildcard matching refA with refB will result in success for all refAs, because “123*”, “123-456*” and “123-456-789*” all match “123-456-789_This_is_a_test”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not exactly a straight-out question because I have just solved it, but
I have a question about best practices regarding how one should approach storing complex
I have this question because I am not familiar latest generation of MS VS
I have to ask this question because I've never programmed except in an intro
I am re-hashing this question because I have looked at over 50 threads in
I'm asking this question because I finally solved a problem that I have been
i reposted this question because i didn't find a good answer. i have a
That might be silly question but I really need to know the answer. If
This is not generic question. I have asked this question because I'm confused with
I'm hesitant to ask this question because of the vagueness of the situation, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.