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


Is there a way to use Wildcards with a LIKE compare statement in SQL

Posted on 2010-01-06
Medium Priority
Last Modified: 2012-05-08
I have a stored procedure where I'm searching a table for Customers Name. SO for example

I have a parameter   @LastName

Is there a way to use the LIKE function with some sort of WildCard?

Such as Select LastName from Customer where LastName LIKE @LastName+'%'

I know that doesn't work but I'm trying to figure out the best way to use soem sort of wildcard so if the user is searching for a customer with lastname of JONES, they can just enter JON and see all the customers whose lastnames begin with JON.

I'm calling this sp from an aspx web page I can probable do the wild card through aspx but if I can just do it through the SP that would be better.


Question by:svasilakos
  • 2
LVL 40

Accepted Solution

Richard Quadling earned 2000 total points
ID: 26196115
That should work fine.

I use that mechanism for many of my fuzzy reports.

The user enters the part of the name they know (say TONY) and they'll get ANTHONY, TONY.

Try this as a straight query (Using query analyzer or MS SQL Studio)...

SELECT @LastName = 'JON'
SELECT LastName FROM Customer WHERE LastName LIKE '%' + @LastName + '%'

What do you get back?


Author Closing Comment

ID: 31673810
I'm kind of an idiot. I didn't even try what I wrote in my question, I just thought I did and didn't think it would work.   That's the ticket...

Thanks for the Fast response.

-Steve, Las Vegas
LVL 40

Expert Comment

by:Richard Quadling
ID: 26196194
Glad to have helped.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

571 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