Link to home
Start Free TrialLog in
Avatar of AfzalHaidry
AfzalHaidry

asked on

How to use LIKE keyword on numeric datatype in a SQL query.

Hi all,
   I have build a generic user control which perform a customized search on data. I am using a datatable namly oDataTable passed to the control. And then I use the select method of oDatatable to search a column data character by character or digit by digit. I have used like keyword in query which is suitable for this situation. It works well when column datatype is char, or varchar. However, when this search performed on a column has numeric datatype then it generates error stating "like cannot be used with Decimal or string datatype".
The code is as follow:

Dim oDataRow as DataRow
oDataRow = oDataTable.Select(ColumnName & " like '" & txtSearch.Text & "%'", ColumnName)

Note: the ColumnName is the Column Name on which I want to perform search and has type numeric.
Note also that if u r going to give me solution as to use ">="  like:
oDataRow = oDataTable.Select(ColumnName & " >= " & txtSearch.Text, ColumnName)

then this will not work because I am going partial searching. This returns all the numbers from a number to be searched. For it returns 9, 10,... for digit 8.
In a nutshell, when I perform search on digit 8, the result should return either 8, if it is not found then 80 or 81, ..., or 89, if these also not found then 800 or 801, ... 899, and so on.

ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or

WHERE CAST(ColumnName AS varchar(12)) LIKE '" & txtSearch.Text & "%'"