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

Posted on 2010-01-06
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
    LVL 40

    Accepted Solution

    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

    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

    Glad to have helped.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now