Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

SOUNDEX(), DIFFERENCE()

These two functions are both for comparing character strings and they’re both pretty useless. By now, they’re just being dragged along into new versions for backward compatibility.

Both DIFFERENCE() and SOUNDEX() are intended to help find matches when strings might be misspelled. SOUNDEX() applies a coding scheme also called Soundex to a string and returns a four-character string consisting of a letter and three digits. The Soundex encoding is meant to give matching results for strings that are phonetically identical or nearly so. So, for example, SOUNDEX(“Smith”) and SOUNDEX(“Schmidt”) both return “S530.” However, the scheme isn’t really good enough to be useful in practice. Among other things, the letter returned is always the first letter of the original string, so “Knowles” and “Noles” return two different values.

DIFFERENCE() takes two strings and returns a number between 0 and 4, measuring the phonetic difference between the strings. A value of 0 means the strings are very different, while 4 means they’re quite similar. Unfortunately, like SOUNDEX(), DIFFERENCE() isn’t very smart. It also doesn’t know about things like “kn” or that “ph” sounds like “f.” We’ve never found a reason to use DIFFERENCE().

If you really need to match strings that might be misspelled, take a look at a terrific library for Visual FoxPro called PhDbase by Korenthal Associates. It handles “fuzzy” search, as well as quick searches of full text. That’s what we use when we need those abilities.

Usage

cReturnValue=SOUNDEX(cString)
nReturnValue=DIFFERENCE(cString1, cString2)

Example

? SOUNDEX("Tamar")   && returns "T560"
? SOUNDEX("ted")     && returns "T300"
? DIFFERENCE("tamar","ted")   && returns 2
? DIFFERENCE("ted","teddy")   && returns 4

See Also

Like()