SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Search with wildcards from Stored proc parameter


Search with wildcards from Stored proc parameter

Author
Message
Digs
Digs
Say Hey Kid
Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)

Group: General Forum Members
Points: 699 Visits: 564
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:-)
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8545 Visits: 7660
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5176 Visits: 4076
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;-)
Digs
Digs
Say Hey Kid
Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)

Group: General Forum Members
Points: 699 Visits: 564
Roger that !, Thanks !:-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search