Skip to content
Advertisement

Check available letters in the middle of word SQL

can someone help me with the next issue. I have words database and I need to check availability of the next letters for substring in the middle of word. For example I have few words which have ‘gr’ in the middle:

***gra****
**gre****
****gri****

so is it possible to get ‘a’, ‘e’, ‘i’ with sql query? I need these letters for create alphabetical suggestions when user try to find words with ‘gr’ in the middle?

The way which is working now for me it’s run sql query for each letter from a-z for check if any word exists in the database. But it’s so many queries and looks weird as for me 🙁

Advertisement

Answer

One simple method is substring_index() and left():

select left(substring_index(col, 'gr', -1), 1)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement