How to use Parameter in SQL WHERE clause? [duplicate]












0















This question already has an answer here:




  • Parameterize an SQL IN clause

    38 answers



  • C# SQL Server - Passing a list to a stored procedure

    7 answers




My question is regarding a SQL server query.



Assume a user types a text "text1 text2 text3...textn" in a search textbox. Now I want to send this to a SELECT's WHERE clause using a parameter.



cmd.Parameters.AddWithValue("@colname", textSearch.Text). 


I want to write in this way



SELECT ... WHERE (colname LIKE text1 OR colname LIKE text2 OR colname LIKE text3 OR ....... colname LIKE textn)  OR (colname LIKE 'text1 text2 text3 ...textn')


How can I do this in SQL?










share|improve this question















marked as duplicate by Nick.McDermaid, Tetsuya Yamamoto, GSerg, Community Nov 23 at 14:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • In trying to work out what you are asking I think the parameter name you are using is confusing things. So I think you want to pass in the text the user types, lets call the parameter @UserText (instead of @ColName), and then in your where clause you would put where colname = @UserText (adjusted to the actual compare you want). I could be wrong of course...
    – Dale Burrell
    Nov 22 at 23:27












  • Use IN instead of OR. Then use this approach: stackoverflow.com/questions/7097079/…
    – Nick.McDermaid
    Nov 22 at 23:32












  • may i use this way WHERE colname IN(@usertext)? It possible to put a variable inside of IN? Or let me check
    – abraham d.
    Nov 23 at 2:18






  • 1




    Don't use addwithvalue
    – SMor
    Nov 23 at 13:24
















0















This question already has an answer here:




  • Parameterize an SQL IN clause

    38 answers



  • C# SQL Server - Passing a list to a stored procedure

    7 answers




My question is regarding a SQL server query.



Assume a user types a text "text1 text2 text3...textn" in a search textbox. Now I want to send this to a SELECT's WHERE clause using a parameter.



cmd.Parameters.AddWithValue("@colname", textSearch.Text). 


I want to write in this way



SELECT ... WHERE (colname LIKE text1 OR colname LIKE text2 OR colname LIKE text3 OR ....... colname LIKE textn)  OR (colname LIKE 'text1 text2 text3 ...textn')


How can I do this in SQL?










share|improve this question















marked as duplicate by Nick.McDermaid, Tetsuya Yamamoto, GSerg, Community Nov 23 at 14:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • In trying to work out what you are asking I think the parameter name you are using is confusing things. So I think you want to pass in the text the user types, lets call the parameter @UserText (instead of @ColName), and then in your where clause you would put where colname = @UserText (adjusted to the actual compare you want). I could be wrong of course...
    – Dale Burrell
    Nov 22 at 23:27












  • Use IN instead of OR. Then use this approach: stackoverflow.com/questions/7097079/…
    – Nick.McDermaid
    Nov 22 at 23:32












  • may i use this way WHERE colname IN(@usertext)? It possible to put a variable inside of IN? Or let me check
    – abraham d.
    Nov 23 at 2:18






  • 1




    Don't use addwithvalue
    – SMor
    Nov 23 at 13:24














0












0








0








This question already has an answer here:




  • Parameterize an SQL IN clause

    38 answers



  • C# SQL Server - Passing a list to a stored procedure

    7 answers




My question is regarding a SQL server query.



Assume a user types a text "text1 text2 text3...textn" in a search textbox. Now I want to send this to a SELECT's WHERE clause using a parameter.



cmd.Parameters.AddWithValue("@colname", textSearch.Text). 


I want to write in this way



SELECT ... WHERE (colname LIKE text1 OR colname LIKE text2 OR colname LIKE text3 OR ....... colname LIKE textn)  OR (colname LIKE 'text1 text2 text3 ...textn')


How can I do this in SQL?










share|improve this question
















This question already has an answer here:




  • Parameterize an SQL IN clause

    38 answers



  • C# SQL Server - Passing a list to a stored procedure

    7 answers




My question is regarding a SQL server query.



Assume a user types a text "text1 text2 text3...textn" in a search textbox. Now I want to send this to a SELECT's WHERE clause using a parameter.



cmd.Parameters.AddWithValue("@colname", textSearch.Text). 


I want to write in this way



SELECT ... WHERE (colname LIKE text1 OR colname LIKE text2 OR colname LIKE text3 OR ....... colname LIKE textn)  OR (colname LIKE 'text1 text2 text3 ...textn')


How can I do this in SQL?





This question already has an answer here:




  • Parameterize an SQL IN clause

    38 answers



  • C# SQL Server - Passing a list to a stored procedure

    7 answers








sql asp.net sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 23:08









Lee Mac

3,30731338




3,30731338










asked Nov 22 at 22:31









abraham d.

65




65




marked as duplicate by Nick.McDermaid, Tetsuya Yamamoto, GSerg, Community Nov 23 at 14:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Nick.McDermaid, Tetsuya Yamamoto, GSerg, Community Nov 23 at 14:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • In trying to work out what you are asking I think the parameter name you are using is confusing things. So I think you want to pass in the text the user types, lets call the parameter @UserText (instead of @ColName), and then in your where clause you would put where colname = @UserText (adjusted to the actual compare you want). I could be wrong of course...
    – Dale Burrell
    Nov 22 at 23:27












  • Use IN instead of OR. Then use this approach: stackoverflow.com/questions/7097079/…
    – Nick.McDermaid
    Nov 22 at 23:32












  • may i use this way WHERE colname IN(@usertext)? It possible to put a variable inside of IN? Or let me check
    – abraham d.
    Nov 23 at 2:18






  • 1




    Don't use addwithvalue
    – SMor
    Nov 23 at 13:24


















  • In trying to work out what you are asking I think the parameter name you are using is confusing things. So I think you want to pass in the text the user types, lets call the parameter @UserText (instead of @ColName), and then in your where clause you would put where colname = @UserText (adjusted to the actual compare you want). I could be wrong of course...
    – Dale Burrell
    Nov 22 at 23:27












  • Use IN instead of OR. Then use this approach: stackoverflow.com/questions/7097079/…
    – Nick.McDermaid
    Nov 22 at 23:32












  • may i use this way WHERE colname IN(@usertext)? It possible to put a variable inside of IN? Or let me check
    – abraham d.
    Nov 23 at 2:18






  • 1




    Don't use addwithvalue
    – SMor
    Nov 23 at 13:24
















In trying to work out what you are asking I think the parameter name you are using is confusing things. So I think you want to pass in the text the user types, lets call the parameter @UserText (instead of @ColName), and then in your where clause you would put where colname = @UserText (adjusted to the actual compare you want). I could be wrong of course...
– Dale Burrell
Nov 22 at 23:27






In trying to work out what you are asking I think the parameter name you are using is confusing things. So I think you want to pass in the text the user types, lets call the parameter @UserText (instead of @ColName), and then in your where clause you would put where colname = @UserText (adjusted to the actual compare you want). I could be wrong of course...
– Dale Burrell
Nov 22 at 23:27














Use IN instead of OR. Then use this approach: stackoverflow.com/questions/7097079/…
– Nick.McDermaid
Nov 22 at 23:32






Use IN instead of OR. Then use this approach: stackoverflow.com/questions/7097079/…
– Nick.McDermaid
Nov 22 at 23:32














may i use this way WHERE colname IN(@usertext)? It possible to put a variable inside of IN? Or let me check
– abraham d.
Nov 23 at 2:18




may i use this way WHERE colname IN(@usertext)? It possible to put a variable inside of IN? Or let me check
– abraham d.
Nov 23 at 2:18




1




1




Don't use addwithvalue
– SMor
Nov 23 at 13:24




Don't use addwithvalue
– SMor
Nov 23 at 13:24

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)