Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi in a table I have the Values like,


123568
568123
156823
745568
568424
156842
115684


I want to filter the values by first letter of last three letters.

for an example
in a text box if i type 5 only "745568" should be displayed.
if I type 8 means 156823,156842has to be displayed..

if I use


select num from numbers where num like '%568%'


. It gives the desired result(filtering by last three letters). but have to get the values with one or two letters are entered.



Plz resolve this.

Thanks in advance.
Posted

Uou can use regular expressions as explained here[^].
 
Share this answer
 
This can be done in several ways. One version:
SQL
... where field like '%5__'

For more info about LIKE, see: http://msdn.microsoft.com/en-us/library/ms179859.aspx[^]
 
Share this answer
 
Comments
palraj001 12-Aug-11 8:00am    
Thanks mika. it helped me.
Wendelius 12-Aug-11 8:28am    
You're welcome, glad it helped :)
palraj001 12-Aug-11 8:32am    
but i need to be do this without a text box. how can i give in dropdownbox itself.
Wendelius 12-Aug-11 8:42am    
Now I didn't quite understand. Do you have a dropdownbox where user can for example select the filter? If so, you can check what is for example the selectindex for the dropdownbox and based on that you can construct the query. For example if you have choices (in the dropdown):
- Third letter from the end is 8
- Third letter from the end is 5
Now if user selects the second item you can use the query like in the solution.
palraj001 12-Aug-11 9:02am    
this how i did.
string myConnStr = ConfigurationManager.ConnectionStrings["testpal"].ConnectionString;

SqlConnection Conn = new SqlConnection(myConnStr);
Conn.Open();
string str;
DataTable dt = new DataTable("tbl");
if (TextBox1.Text.Length==1)
{
str = "SELECT num FROM numbers where num like '%" + TextBox1.Text + "__'";
}
else if (TextBox1.Text.Length == 2)
{
str = "SELECT num FROM numbers where num like '%" + TextBox1.Text.Substring(0,1) + "" + TextBox1.Text.Substring (1, 1) + "_'";
}
else
{
str = "SELECT num FROM numbers where num like '%__" + TextBox1.Text + "'";
}

SqlCommand comm = new SqlCommand(str, Conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
Conn.Close();
DropDownList1.DataSource =dt;
DropDownList1.DataTextField="num";
DropDownList1.DataValueField="num";
DropDownList1.DataBind();




i type in textbox. result will be showed in dropdownbox.is there any possibility type values in dropdownbox itself?

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