I am wanting to know if there is a way, in Oracle SQL, to compare two strings for similarities other than =
or like
. For instance, SQL Server has a function difference(str1, str2)
that compares the two strings and gives a similarity rating (0 to 4). Not exactly what I want but that would still be extremely helpful.
I was hoping specifically for functions or methods that would:
- Compare a string character by character (return the number of exact matches)
- Tell how many characters are in one string, but in the wrong place
The primary use would be for strings of same length, that contain numbers (IDs, phone numbers, etc.) For my purposes, I would use it to find possible matches in which letters/numbers may have been transposed. Soundex(string)
works well for alpha strings but seems to ignores numbers (for good reason).
I do not have privileges to create functions on my own, but if someone knows a method of doing that I would interested in hearing it. One solution to compare char-to-char (that only works if you know the MAX number of characters) is:
--For char-to-char counts
Decode(substr(ID1,1,1), substr(ID2,1,1), 1, 0) +
Decode(substr(ID1,2,1), substr(ID2,2,1), 1, 0) +
[...]
Decode(substr(ID1,N,1), substr(ID2,N,1), 1, 0)
But that is about as inelegant as you can get.
Any help would be greatly appreciated.