LIKE clause with Numeric Data

Posts   
 
    
BTV
User
Posts: 2
Joined: 05-May-2010
# Posted on: 05-May-2010 17:21:59   

I want to get data filtered by a numeric value beginning by a value. For example, if the value is "5", result will be data row where numeric value is 5, 50,56,500,...

To do that, I programmed the linq query :

var query = from xref in lnq.MyTable where xref.MyNumericColumn.ToString().StartsWith("5") select xref;

But when running, the where clause is ignored and all the table content is returned. How should i do ? Thanks for your help.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 05-May-2010 17:57:53   

So you have a numeris column, which contains numbers say from 1 to 1000. And you want all rows starting with 5 for instance, right? This way you can get the following values back: 5, 50, 51, 52, 53, ....500, 501, 502,......,599.

Correct?

I think this can be done using database functions to convert the numeric value to string and then you can apply the LIKE filter on them.

For example (using Northwind):

SELECT * FROM Products
WHERE CONVERT(varchar, UnitPrice) LIKE '5%'

And for this you can easily use a DBFunctionCall, to produce the CONVERT() expression.

BTV
User
Posts: 2
Joined: 05-May-2010
# Posted on: 05-May-2010 18:01:49   

That's exactly what I want to do. Is there a solution using Linq ? Thanks for your answer.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 05-May-2010 18:31:13   

Please try using Function mappings