Skip to content
Advertisement

Postgres Compare string to number return correct result

Trying to understand how postgres compare string to number because following queries return true,

Can someone please explain?

Advertisement

Answer

PostgreSQL applies implicit conversion where necessary and possible. How it does this is no further explained as far as I know. (https://www.postgresql.org/docs/9.1/typeconv.html)

Anyway, we can play a little with your queries and find out what’s happening:

would result in true for SELECT '12' BETWEEN '1' and '2' and false for SELECT 12 BETWEEN 1 and 2. Run it. It returns false.

Here is a further test:

This fails with ERROR: invalid input syntax for type integer: "A".

So, PostgreSQL tries to convert the strings to numbers, because it thinks you want to deal with numbers here.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement