Search with wildcards from Stored proc parameter

  • I wish to allow the user to load in from a control the search input for the wildcard search in a SELECT query in a TSQL stored procedure.

    MyStoredProc(

    @SearchContains VARCHAR(20)=NULL,

    @SearchStartsWith VARCHAR(20)=NULL)

    NOTE: Field 'OrderName' is a VARCHAR field

    SELECT OrderID, OrderName, OrderValue FROM <table>

    WHERE OrderName = @SearchContains

    or

    SELECT OrderID, OrderName FROM <table>

    WHERE OrderName = @SearchStartsWith

    I wish for the @SearchStartsWith (or @SearchContains) to be the input to go in between the wild card parameters of WHERE clause search, but I dont know the parameters to get the job done in the two search examples above, please advise, thanks:-)

  • MyStoredProc(

    @SearchContains VARCHAR(20)=NULL,

    @SearchStartsWith VARCHAR(20)=NULL)

    NOTE: Field 'OrderName' is a VARCHAR field

    SELECT OrderID, OrderName, OrderValue FROM <table>

    WHERE OrderName like '%' + @SearchContains + '%'

    or

    SELECT OrderID, OrderName FROM <table>

    WHERE OrderName like @SearchStartsWith + '%'

    or

    SELECT OrderID, OrderName FROM <table>

    WHERE LEFT( OrderName, LEN(@SearchStartsWith)) = @SearchStartsWith


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Craig Farrell (9/2/2010)


    SELECT OrderID, OrderName FROM <table>

    WHERE LEFT( OrderName, LEN(@SearchStartsWith)) = @SearchStartsWith

    this is bad from performance perspective , never use function with where caluse column , it will force sql optimizer to NOT TO use index

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Roger that !, Thanks !:-)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply