Hi Klaus,
the answer to why is in the help topics about SET EXACT and SET ANSI ON/OFF or more generally the way VFP compares strings and evaluates them as equal or different. At default VFP only compares partial strings, so that "32"="3" is true, but at the same time it's not transient (you cannot swap and get the same boolean result), "3"="32" is false.
But even without knowing these details, there is a simple way to see why group by does not fail: The group by is done by the whole field, while code like SUM FOR itemcode="3" a) doesn't group for all itemcodes but just picks out the sum for itemcode "3" and b) isn't the full length itemcode "3 ". Whereas group by taking the full field length it groups by "3 " and "32" separately and those strings definitely differ no matter how strings are compared when they have different lengths, all strings in char fields by definition have the same length and when the padding spaces are taken into account, you don't misgroup.
That's why SUM ... FOR itemcode="3 " then works. Or whatever length itemcode has.
You can, indeed, reintroduce the same problem into SQL when you add a where clause that compares with a partial itemcode.
Code:
Select itemcode, sum(quantity) as total From sales group by itemcode where itemcode="3"
Doing that you also get the sums for both itemcode "3" and "32", but SQL saves your back, if you look into the result:
The where clause also fetches both itmecodes, so you get more than you expect, but group by makes two groups from the data and you still get the group you actually wanted and you can understand your comparison isn't as specifically picking out only one itemcode but all itemcodes starting with "3".
And the essence of it all, only use constant length identifiers when using strings. And, well, you could also say better not use strings for identifiers, but numbers. Comparison of numbers is more straight forward for what is equal, though floating point also has many gotchas in that respect.
The reason string comparisons work that weirdly also is easy to explain, as VFP is mainly for data processing and within databases before the introduction of variable length fields ou still wanted to find some Mr. Smith by writing WHERE lastname="Smith" and not WHERE lastname = PADR("Smith", len(lastname)) or even worse manually padding with spaces which would be code that only works as long as the field has the same length.
Think alone of the fact that SQL was meant to be nearer to natural language than usual code. Then it would sabotage that purpose if the user of SQL has to think about such details in the queries. VFP by default differs from how the ANSI standardization of SQL defined string comparisons, with SET ANSI ON you can also use the "too simple" where clause and only get one group. Same SQL, just after SET ANSI ON:
Is it therefore recommended to SET ANSI ON? It depends. But there are good reasons for VFP to differ from the ANSI standard in that respect. So it's not a bugfix that you can turn on or off, it's a design decision that goes into aspects of how the indexes are organized and rushmore optimization, I guess, too.
Chriss