Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

Wildcard in subquery

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
mixxie
Asked:
mixxie
  • 7
  • 6
  • 4
  • +1
3 Solutions
 
TimCotteeCommented:
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
 
Rey Obrero (Capricorn1)Commented:
you can try this one

        where Instr([txtTrailer],[txtTrailerID])>0
0
 
mixxieAuthor Commented:
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
Rey Obrero (Capricorn1)Commented:

reversed; try this

where Instr([txtTrailerID],[txtTrailer])>0
0
 
mixxieAuthor Commented:
Nope, that didn't do it either... it returned the same recordset. :-(

Am I doing the subquery correctly?
0
 
Gustav BrockCIOCommented:
> 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
 
Rey Obrero (Capricorn1)Commented:
can you post sample data from both tables.

maybe we could use Left(txttrailerID,7)=  Left(txttrailer,7)
0
 
mixxieAuthor Commented:
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
 
Rey Obrero (Capricorn1)Commented:
instr([tblTrailer].[txtTrailer],[tblOrders].[txtTrailerID])>0

should do it
0
 
Gustav BrockCIOCommented:
I doubt that Capri.
But what about your previous suggestion:

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

/gustav
0
 
mixxieAuthor Commented:
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
 
Rey Obrero (Capricorn1)Commented:
try this

SELECT tblOrders.intOrderID, tblOrders.txtTrailerID
FROM tblOrders, tblTrailers
WHERE (((tblTrailers.txtTrailer) Like [tblorders].[txtTrailerID] & "*"))
0
 
Rey Obrero (Capricorn1)Commented:
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
 
mixxieAuthor Commented:
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
 
Gustav BrockCIOCommented:
Michelle, Capri has a short memory.
He should have used his first suggestion (sligthly modified):

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

/gustav
0
 
Rey Obrero (Capricorn1)Commented:
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
 
Gustav BrockCIOCommented:
OK. So how about

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

/gustav
0
 
mixxieAuthor Commented:
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 7
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now