?
Solved

Wildcard in subquery

Posted on 2005-02-25
18
Medium Priority
?
500 Views
Last Modified: 2008-03-10
Hello, experts!  I'm trying to do a wildcard in a subquery:

SELECT tblOrders.intOrderID
FROM tblOrders
WHERE (((tblOrders.txtTrailerID) In (Select txtTrailer From tblTrailers As T Where T.txtTrailer Like tblOrders.txtTrailerID)));

But I want to return all values where tblOrders.txtTrailerID contains any matching part of T.txtTrailer.  For example, I want to return tblOrders.txtTrailerID = trailer1 when there is a record for tblTrailers.txtTrailer = trailer12.  I hope that makes sense.  I've tried "... Where T.txtTrailer Like * tblOrders.txtTrailerID *", and variations thereof, and received errors.  

Please advise.

Thanks!

Michelle
0
Comment
Question by:mixxie
[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
  • 7
  • 6
  • 4
  • +1
18 Comments
 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 200 total points
ID: 13404250
Hi mixxie,
> SELECT tblOrders.intOrderID
> FROM tblOrders
> WHERE (((tblOrders.txtTrailerID) In (Select txtTrailer From tblTrailers
> As T Where T.txtTrailer Like '*'+tblOrders.txtTrailerID+'*')));

Should do it.


Tim Cottee
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 13404438
you can try this one

        where Instr([txtTrailer],[txtTrailerID])>0
0
 

Author Comment

by:mixxie
ID: 13404613
Thanks for the quick responses.  :-)

I've tried both of those methids, and both allow me to run the query without the error.  However, I'm not getting what I'm expecting.  I know, for instance, that the recordset should return trailer1 because tblTrailers contain trailer12, but it's only returning records with trailer12.  Any idea what I'm doing wrong?

Thanks,

Michelle
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 13404676

reversed; try this

where Instr([txtTrailerID],[txtTrailer])>0
0
 

Author Comment

by:mixxie
ID: 13404704
Nope, that didn't do it either... it returned the same recordset. :-(

Am I doing the subquery correctly?
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 13404713
> I want to return tblOrders.txtTrailerID = trailer1 when there is a record for tblTrailers.txtTrailer = trailer12.  I hope that makes sense.

Sorry, no.
Like *..* would return it the other way round:

  .. return tblOrders.txtTrailerID = trailer12 when there is a record for tblTrailers.txtTrailer = trailer1.

That would make sense.

/gustav
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 13404717
can you post sample data from both tables.

maybe we could use Left(txttrailerID,7)=  Left(txttrailer,7)
0
 

Author Comment

by:mixxie
ID: 13404808
What I'm trying to do is cross-reference a table that I just created from a spreadsheet a customer sent me.  The customer wants to see all order numbers that are associated with certain trailer numbers.  The problem is that I'm finding some trailer records do not contain the full trailer ID, which is what was sent in the spreadsheet.  So now I have two tables:

tblOrders
-------------------------------
intOrderID            txtTrailerID
-------------------------------
20136747            nyku250423
20136801            nyku2504231


tblTrailer
-----------
txtTrailer
-----------
NYKU2504231

Right now, I'm only getting 1 record returned.  I need to return both.  I apologize for the confusion.

Thanks, guys!  :-)

Michelle
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 13404832
instr([tblTrailer].[txtTrailer],[tblOrders].[txtTrailerID])>0

should do it
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 13404888
I doubt that Capri.
But what about your previous suggestion:

maybe we could use Left(txttrailerID,10)=  Left(txttrailer,10)

/gustav
0
 

Author Comment

by:mixxie
ID: 13404898
Still no go :-(

here's what I've got:

SELECT tblOrders.intOrderID, tblOrders.txtTrailerID
FROM tblOrders
WHERE (((tblOrders.txtTrailerID)
IN (Select txtTrailer From tblTrailers
WHERE instr([tblTrailers].[txtTrailer],[tblOrders].[txtTrailerID])>0
)));

using what you just gave me.  It's still just returning the 1 record.

Thanks! :-)

Michelle
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 1400 total points
ID: 13405033
try this

SELECT tblOrders.intOrderID, tblOrders.txtTrailerID
FROM tblOrders, tblTrailers
WHERE (((tblTrailers.txtTrailer) Like [tblorders].[txtTrailerID] & "*"))
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 13405064
if you still have problem try this

WHERE (((tblTrailers.txtTrailer) Like Trim([tblorders].[txtTrailerID]) & "*"))

and better run an update to your table like

Update tblOrders set txttrailerId=trim(txttrailerid)
0
 

Author Comment

by:mixxie
ID: 13405364
Capricorn1,

Both of those solutions return an enormous amount of records, i.e.

intOrderID           txtTrailerID
-------------------------------
20136700          
20136700          
20136700
20136700
..........

where the txtTrailerID field in tblOrders is null.  I wasn't able to see if it includes any orders where the txtTrailerID field is not null because the query ends up freezing.  

If I do a left join on it, it returns the same recordset that was returned before.

Thanks! :-)

Michelle
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 13405418
Michelle, Capri has a short memory.
He should have used his first suggestion (sligthly modified):

.. use Left(txttrailerID,10) = Left(txttrailer,10)

/gustav
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 13405465
gustav,
not really<lol> i was just thinking that the lentgh or format is not uniform.

michelle,
now you are getting plenty of records return <joke><lol>

which did you run, the one with Trim or the one without?
0
 
LVL 51

Assisted Solution

by:Gustav Brock
Gustav Brock earned 400 total points
ID: 13405531
OK. So how about

WHERE Left(txtTrailerID, Len(txtTrailer)) = txtTrailer

/gustav
0
 

Author Comment

by:mixxie
ID: 13405662
capricorn1,

It's working!  I figured out what was happening.  Your solution worked fine, but I have a decent amount of null values in the txtTrailerID field in tblOrders.  For each null value, it was producing a record for each trailer ID held in tblTrailers, which resulted in the huge amount of records and the freezing.  Once I limited the query to "not null" values, it works perfectly.  Thanks, everyone, for all of your help!!! :-)

Michelle
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

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