Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL query that concanate 2 fields for 1 search

Posted on 2013-01-10
13
Medium Priority
?
417 Views
Last Modified: 2013-01-31
Hi experts,
I have a database that have these 2 fields, name and firstname, and I want to do a query from one field that MIGHT INCLUDE both fields or JUST ONE
EX.
     Name                         FirstName
1   John                            Doe
2   George Wayne
3   John Doe
4   George                        Wayne

I would like to search for John, and get record 1 and 3
Search John Doe and get record 1 and 3
Search Doe and get record 1 and 3

What I am able to do with the code below is : If I search John Doe, I will only get record 3, but I want to be able to get record 1 TOO !!! (Doe is part of FirstName)

strSQl = "SELECT name, firstname FROM customers WHERE (name LIKE '*" & dt(0).Text & "*') OR (firstname LIKE '*" & dt(0).Text & "*')"
strSQl = strSQl + " order by main"


*** Also what would I need to insert IF I DON'T WANT TO INCLUDE DUPLICATES

Thanx
0
Comment
Question by:crescue
[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
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 38765283
What exactly do you want to see from the data posted?
Is dt(0).Text {first name] or [name]?
0
 

Author Comment

by:crescue
ID: 38765318
dt(0) is the input field that I would like to search from
dt(0) might be JOHN or DOE or JOHN DOE
I want to use that dt(0).text to look into {first name] or [name]
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38765352
I have that.
So you want to see
John Doe
once

This is without criteria.

SELECT Trim(nz([firstName],"") & " " & Replace([name],nz([firstname],""),"")) AS Expr1
FROM Customers
GROUP BY Trim(nz([firstName],"") & " " & Replace([name],nz([firstname],""),""));
Assumes no blank [name]
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 26

Expert Comment

by:jerryb30
ID: 38765383
This is fraught with possibilities for errors, since repeats of first and last name could cause some records not to show.
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38765392
Oh, and I did the one for no duplicates.
Is your criteria working otherwise?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38766474
I think this will do the trick too:

strSQL = "SELECT [Name] & ' ' & FirstName FROM Customers WHERE [Name] & ' ' & FirstName  LIKE  '*" & dt(0).Text & "*'"

Open in new window



Not sure how you are defining 'duplicates'.  Are rows 1 and 3 duplicates, or are they different?
0
 

Author Comment

by:crescue
ID: 38767126
rows 1 and 3 are different
Nope, it is not working the way I want it to work (according to the code provided)
Basically what I want is :

There is NO 'JOHN DOE' EITHER IN [NAME] nor [FIRSTNAME]
There is 'DOE' in [FIRSTNAME] and 'JOHN' in [NAME]

**** But I want to search for 'JOHN DOE' and STILL GET IT
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 38767180
Okay...

Try this:

dim s()
dim x
dim strSQL
dim strWhere
dim strRoot

s = split(dt(0).Text, " ")
strRoot = " [Name] & ' ' & FirstName "

for each x in s
       strWhere = strWhere & strRoot & "LIKE '*" & x  & "*' OR"
Next
if Right( "nn" & strWhere, 2) = "OR" then strWhere = Left(strWhere, Len(strWhere) - 2)
strSQL = "SELECT [Name] & ' ' & FirstName FROM Customers WHERE " & strWhere

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 38767201
btw, what are you using for your code?  Access VBA, VB.Net, VBScript...?

dt(0).Text looks unusual for Access VBA.
0
 

Author Comment

by:crescue
ID: 38767273
VB
0
 
LVL 32

Expert Comment

by:awking00
ID: 38767443
strSQl = "SELECT name, firstname FROM customers WHERE instr(name & " " & firstname,dt(0).Text) > 0
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38768004
try:
SELECT Customers.firstName, Customers.Name, Customers.Main
FROM Customers
WHERE (((Trim(nz([firstName],"") & " " & Replace(nz([name],""),nz([firstname],""),""))) Like "*" & [entername] & "*")) OR (((Customers.firstName) Like "*" & [entername] & "*")) OR (((Customers.Name) Like "*" & [entername] & "*"))
ORDER BY Customers.Main;
where [entername] is your criteria
0
 

Author Closing Comment

by:crescue
ID: 38841372
Thqnx
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

636 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