This is a migrated thread and some comments may be shown as answers.
Compare varchar field with int
3 Answers 19 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ratndeep
Top achievements
Rank 1
Ratndeep asked on 01 Feb 2010, 10:50 AM
Hi ,
I have to compare a varchar field with a int value .How can i do this.Plz Guide.

Thanks...

3 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 04 Feb 2010, 08:23 AM
Hello Ratndeep,

I assume that the VarChar value is mapped to a string field of a persistent class. In order to compare the two values, they have to be from the same type. You have two options - to convert the string value to integer or convert the integer value to string. You did not mention which way you want to go, so I will show you both of them. In both cases you can use the CompareTo method which returns an integer indicating whether the first value is less, equal or greater than the second:
string stringValue = "123";
int intValue = 124;
 
//convert the string to integer
int intFromString;
if (int.TryParse(stringValue, out intFromString))
{
    int result1 = intFromString.CompareTo(intValue);
}
 
//convert the integer to string
int result2 = stringValue.CompareTo(intValue.ToString());

Please let us know if this is not what you are looking for and try to give some more details on what you are trying to achieve.

Best wishes,
Alexander
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Ratndeep
Top achievements
Rank 1
answered on 04 Feb 2010, 10:40 AM

Sorry for my previous post.It was highly unclear.
In my following query i have to apply relational operator (<=,>=) on varchar field .
here x.Origin is varchar filed  and originId  is a integer value.How can i achieve this.

 

 

return (from x in Table where x.Origin <= originId && x.Destination >= destinationId select x).SingleOrDefault();

 

0
Alexander
Telerik team
answered on 09 Feb 2010, 11:54 AM
Hi Ratndeep,

I see only two available options here. If your OriginId values have always the same length, you can compare them as strings, the result would be the same as if comparing them as integers. In this case the query would look like this:
from x in scope.Extent<Table>()
where x.Origin.CompareTo(originId.ToString()) <=0 && x.Destination >= destinationId
select x

However, if they have different length this would not work correctly and you will have to cast the string value to an integer before the comparison. Unfortunately the necessary int.Parse() method cannot be handled by OpenAccess currently and you will have to do the filtering in memory. This would look similar to this:
from x in scope.Extent<Table>().ToList()
where int.Parse(x.Origin) <= originId && x.Destination >= destinationId
select x
Hope that helps.

All the best,
Alexander
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
Tags
General Discussions
Asked by
Ratndeep
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Ratndeep
Top achievements
Rank 1
Share this question
or