String comparison
  • Vince
    String comparison

    by Vince » Wed Jul 12, 2006 11:19 am

    Capital vers 7.4
    While creating a report on a card file I recently found that a targeted record was not being included in the result.
    The cardfile has a field of two chars that are used as alphabetic codes. A normal string comparison [UPPER(ALLTRIM(Field)) = UPPER(var)] was not apparently returning TRUE when expected. I tried not a few variations on the above code, but nothing worked.
    Finaly I converted both the field and the var to transactions [AsTran()] and the comparison worked correctly, which was expected as the field is only two chars in length. What I am wondering now is can I rely on this method when the field size is considerabley larger? Certainly larger than 9 chars.
    I know that string comparison can be unreliable in some circumstances and wonder if there is a trick here I am unaware of?
    TIA
    Vince
  • Vince

    Further to

    by Vince » Wed Jul 12, 2006 2:14 pm

    I should perhaps mention that I did try "UPPER(TRIM(fieldstring)) == UPPER(varstring)" :)

    Vince
  • COBS Tech Support
    Posts:683
    Joined:Fri Sep 09, 2005 8:23 am

    by COBS Tech Support » Thu Jul 13, 2006 2:03 pm

    There are a few basics things to watch for when construcitng comparison statements and when dealing with a computer.

    (1) Computers treat upper and lower case differently. So "H" does not equal "h". Hence, it's important to use the Upper() function when you are not absolutely sure that both items being compared are always going to be in upper case. The Upper() function converts all characters to upper case for the purposes of the comparison.

    (2) Computers may treat strings of unequal length as different. For example, "Hello" and "Hello " may not match because there is an extra space (not visible to humans) at the end of the second variable. To eliminate extra spacing using the Alltrim() function on all variables or database fields being compared.

    (3) There are two equivalence operators in CBS. The "=" means match closely and "==" means "match exactly". You will never get a match if you use the "==" operator with two values of different length.

    Hence,

    Upper(Trim(fieldstring)) = Upper(varstring)

    May work, depending on the values of the variables.

    But this will always work if both variables are the same:

    Upper(Alltrim(fieldstring)) == Upper(Alltrim(varstring))

Who is online

Users browsing this forum: No registered users and 26 guests