Compare varchar field with int

Thread is closed for posting
4 posts, 0 answers
  1. Ratndeep
    Ratndeep avatar
    40 posts
    Member since:
    Feb 2009

    Posted 01 Feb 2010 Link to this post

    Hi ,
    I have to compare a varchar field with a int value .How can i do this.Plz Guide.

    Thanks...
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 04 Feb 2010 Link to this post

    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.
  3. Ratndeep
    Ratndeep avatar
    40 posts
    Member since:
    Feb 2009

    Posted 04 Feb 2010 Link to this post

    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();

     

  4. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 09 Feb 2010 Link to this post

    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.
Back to Top