Question How to use Like to search an integer

viper5646

Active member
Joined
Jan 20, 2009
Messages
38
Location
Canada
Programming Experience
Beginner
hello.

I have used Like operator to search for String data with no problem.
Now I am trying to search 5 numeric integer. Beginning with the first number entered then the second and so on. ex. if I have a number 12345 when the user enters "1" every number starting with 1 will display if entered "12" every number starting with 12 will be displayed and so on.
I have tried some code but it returs the folowing error:" Cannot perform 'Like' operation on system.int32 and system.string.

this is my code:
VB.NET:
 Private Sub srchDesign_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles srchDesign.TextChanged
        strSearch = srchDesign.Text

        designview.RowFilter = "design Like'*" & (strSearch) & "*'"
       
    End Sub
 
You could convert the field to a varchar.

VB.NET:
WHERE CONVERT(varchar(50), NumericField, 0) LIKE @field +'%'
 
rcombs4 has the right idea but, as you're doing this in the RowFilter of a DataView and not in SQL code you need to use slightly different syntax:
VB.NET:
designView.RowFilter = String.Format("CONVERT(design, 'System.String') LIKE '{0}*'", strSearch)
Noptice that there's no wildcard before the value, only after. That's because you said you wanted matches that start with the search value, not matches that contain the search value anywhere.
 
thank you Rcombs4 and Jmcilhinney for your reply.
yes Rcombs4 code might work using a SQL. But it's Jmcilhinney's code that I need because it works. I'll keep Rcombs4 for future reference.
 

Latest posts

Back
Top