I have a postgres table that contains a very large number of numerals of various lengths. For example, the first column of the table will look like this:
- 1
- 12
- 13
- 134
- 135
- 136
- 1362
- 1363
- …
I need postgres to return the row that matches as much of the beginning of a given numeral as possible. For example, the numeral “1358302” needs to match row 5 (“135”) and “1362304” needs to match row 7 (“1362”).
What SQL could I use to achieve this?
(the numerals aren’t sorted there permanently yet, so if you need to switch the data type between integer or string for simplicity/performance sake, I can still do that!)
1 Answer