Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to use both like and In clause in sql command

[edit]Tags only - OriginalGriff[/edit]
Posted
Updated 29-Mar-11 22:01pm
v2

The LIKE clause is very useful when you want to specify a search condition within your SQL WHERE clause, based on a part of a column contents where as The IN clause allows you to specify discrete values in your SQL WHERE search criteria.
take a look at given link SQL_Like[^] and SQL_IN[^] to read an excellent articles which embedded good examples.
 
Share this answer
 
Comments
manasBonton 30-Mar-11 4:22am    
//i want to take a sentence in textbox and spliting and searching and displaying if related word is there.now it is displaying if accurate word will give like TV but if Tvor Tvv means not displaying.
kindly answer me sir
/////////////////
string commaDelimited = textBox1.Text;
string[] year = commaDelimited.Split(new char[] { });

/////////////////////////////////////////
//string[] r = { "TV", "Radio", "Coal" };
string filter = string.Empty;
foreach (string name in year)
{
if (filter.Length > 0)
{
filter += ",";
}
filter += string.Format("'{0}'", name);
}
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=ERPDB;Integrated Security=True");
SqlDataAdapter adp;
DataSet ds = new DataSet();
adp = new SqlDataAdapter("Select * from Prod_Tbl where ProductName IN (" + filter + ")", con);
adp.Fill(ds, "Prod_Tbl");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
Without the context, it is not easy to answer. Still, here is something to start with.

Select Col1 from table1 where Col2 in ('value1','value2') and col3 like 'val%'


Select Col1 from table1 where ((Col2 in ('value1','value2')) or (col2 like 'Other%'))



Update: You try it this[^] way.
 
Share this answer
 
v2
Comments
manasBonton 30-Mar-11 4:23am    
//i want to take a sentence in textbox and spliting and searching and displaying if related word is there.now it is displaying if accurate word will give like TV but if Tvor Tvv means not displaying.
kindly answer me sir
/////////////////
string commaDelimited = textBox1.Text;
string[] year = commaDelimited.Split(new char[] { });

/////////////////////////////////////////
//string[] r = { "TV", "Radio", "Coal" };
string filter = string.Empty;
foreach (string name in year)
{
if (filter.Length > 0)
{
filter += ",";
}
filter += string.Format("'{0}'", name);
}
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=ERPDB;Integrated Security=True");
SqlDataAdapter adp;
DataSet ds = new DataSet();
adp = new SqlDataAdapter("Select * from Prod_Tbl where ProductName IN (" + filter + ")", con);
adp.Fill(ds, "Prod_Tbl");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
dan!sh 30-Mar-11 4:50am    
Updated reply. Also, do not repost same question again.
manasBonton 30-Mar-11 6:09am    
you try to give the correct answer and try to understand my question
dan!sh 30-Mar-11 12:43pm    
i you cannot edit the SQL in the link I have posted or if you cannot figure out/search a better way, you are not capable of what you have taken up. I am sorry to be rude.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900