I have an integer column and I need to search for rows in which the said column starts with 19
In MySQL I would use SELECT ... WHERE id
LIKE \'19%\'
For all positive integers >= 10, you can do this with no TEXT/VARCHAR necessary:
SELECT * FROM "table"
WHERE $1 = id / (10 ^ (floor(log(id)-1)))::integer
You can index this, as well:
CREATE INDEX idx_table_2_digits
ON "table" ( 10 / (id ^ (floor(log(id)-1)))::integer);
The calculation basically works like this:
This algorithm could even be generalized into a function, f(x,y), where x is the id and y is the number of leading digits to return:
# python-esque pseudocode:
def integer_leading_digits (id, digits):
if id < (10 * (digits - 1)):
return -1 # error condition
return id DIV int(10 ^ (floor(log(id)-(digits-1))))