Luke101
Luke101

Reputation: 65278

How do I query a VARCHAR as an integer

I have a column that holds VARCHARs and I have a flag that determines if the value is a string or integer. I would like perform a search on the integers but they are stored as VARCHAR. How do I do this?

I am trying to do this in a where clause.

var q = from x in context.table
        where x.Criteria > 0
        select new
        {
              x
        }

Upvotes: 0

Views: 559

Answers (3)

GWB
GWB

Reputation: 2605

You'll probably need to pull down the values from the server as strings and do the conversion in LINQ to Objects.

var q = context.table
    .Where(x => x.Criteria > 0)
    .Select(x => x.IntOrStringValue)
    .AsEnumerable()
    .Select(v => Convert.ToInt32(v));

replace x.Criteria > 0 with your logic that indicates the value is an integer. replace x.IntOrStringValue with your column containing the integers.

Upvotes: 1

Holystream
Holystream

Reputation: 972

sql

select isnull(Convert(int, varcharColumn),0)
from atable
where isInteger = 1

linq

var q = context.YourCustomView;

Upvotes: 1

dan04
dan04

Reputation: 91189

CAST(TheColumn AS INTEGER)

Upvotes: 1

Related Questions