views:

166

answers:

1

Hey guys,

I am trying to use the Contains operator. It works fine with test data eg.
WHERE CONTAINS(file,'"*ash*"')

However, I want to get the keyword from a TextBox using something like CONTAINS(file,'"*@key*"'), but this doesnt seem to work. Any suggestions please.

Thanks

+3  A: 

The @ is used to declare a variable in SQL. For this reason, it doesn't need to be inside quotes. For example:

DECLARE @Key varchar(100);
SET @Key = '*ash*'; 
SELECT file FROM SomeTable WHERE CONTAINS(file, @Key);

EDIT: If you are passing inline SQL through your code then, I would suggest something like this:

C# Example:

string key = textBox1.Text;
string query = "SELECT file FROM SomeTable WHERE ";
query += String.Format("CONTAINS(file, '\"*{0}*\"')", key);
Jose Basilio
hey thanks for your reply but can this work in a sqldatasource? because that is where i am actually trying to do it. Also i try using it without double quotes in sql server and it only works with the double quotes i feel i am doing something wrong. but the mission is to do this in a sqldatasource to display in a gridview.
well i am using this as test in a sqldatasource SELECT article_title, author, publish_by, publish_date, [file] FROM tb_article WHERE (category = @cat) AND CONTAINS (file_name, '"*a*"') and that works but this is test i actually want the keyword to use for searching to come from a textbox, i tried CONTAINS(file_name,'"*' + @key + '*"') but doesn't work getting error near '+' can't parse text. The @key would be define parameter as textbox1.text in the next step.Are you understanding what I am saying or I confusing you?
I also tried CONTAINS(file_name,'*a*') but nothing returns not getting an error but no values return that is why wheni tried the double quotes it works, apparently when you use the double quotes it indciates that * is used as a wild card.
please help me, in desperate times
string key = textBox1.Text;string query = "SELECT file FROM SomeTable WHERE ";query += String.Format("CONTAINS(file, '*{0}*')", key); hey i want to use "" can i use "CONTAINS(file,'"*{0}*"')"
ok great thanks go ahead ignore last comment
Please see my Edited code again. I added a \" to escape the quotes
Jose Basilio
hey thanks so much for your reply however it is coming out like this CONTAINS(file,"'*word*'") when i display it in a message box but it shud be '"*word*"' cause when i try the code you send me nothing returns. i'm sorry to cause you so much trouble
can this be done in a sqldatasource?
If you are setting the SELECT statement in the codebehind, it should work. However, this does not work in the markup.
Jose Basilio
hey actually yes it works, my where clause for date was changing around the dates. thanks alot. feel like 1/4 a boss lol thanks!