Sort the table by the closest value of the parameter passed in stored procedures

0

How to sort a table according to the approximate value of a parameter passed in a stored procedure

Ex:

Let's say in a table the name field has the following values: Tiago, Iago and Thiago and I pass as a parameter in the stored procedure "Thiag", it should present me the data in the following order:

  • Thiago
  • James
  • Iago
  • Does anyone know anything that can help me?

        
    asked by anonymous 18.03.2016 / 20:19

    1 answer

    0

    One solution to postgres would be as follows:

    with ldist as (
       select name, 
              levenshtein(name, nameParametro)  as distance
       FROM names
    ) 
    select * 
    from ldist
    order by distance;
    

    If you are using another database you will have to adapt it.

        
    18.03.2016 / 20:30