[Answered ]-Sql query or django filter for string

2👍

In postgres exists a function for similarity, first create the extension that contains it

create extension pg_trgm;

after that select using a percentage of similarity like this (e.g. 80% or your desired value)

select x from "table" where similarity('abcdefghijklmnop', x)>0.35;

or the most similar string would be

select * from "table" order by similarity('abcdefghijklmnop', x) desc limit 1;

Fiddle http://sqlfiddle.com/#!17/c901e/2 and docs https://www.postgresql.org/docs/current/pgtrgm.html

-1👍

best one i found is this:

SELECT id, 
FROM table        
WHERE '3552' LIKE prefix || '%'        
ORDER  BY prefix DESC        
LIMIT  1

thank you all

Leave a comment