Chan
Chan

Reputation: 4301

How to search SQL when words contain errors

I would like to execute a SQL command. However, the keywords may contain errors. For example, the correct command should be

select id from my_table where name = 'Tommy'

It would return 1.

However, if someone execute the following incorrect command:

select id from my_table where name = 'Tomyy'

How to change the command so that it still returns 1?

Thanks a lot.

Upvotes: 0

Views: 229

Answers (2)

Anson Aricatt
Anson Aricatt

Reputation: 393

Try this

select id from my_table where SOUNDEX(name) = SOUNDEX('Tomyy')

Upvotes: 0

Jens
Jens

Reputation: 3299

There are many ways to tackle these, but please keep in mind this isn't the easiest of tasks. What you're looking for is a fuzzy search algorithm.

This should get you started: Fuzzy searches in SQL Server (Redgate) Code project also has some interesting options here: Implementing phonetic name searches

If you're looking for an easier but more barebones solution you should look into using SOUNDEX or DIFFERENCE (assuming your dbms is MSSQL). I've been playing a bit with DIFFERENCE and it's pretty cool what this can do out of the box.

Upvotes: 2

Related Questions