Search code examples
postgresqlfractions

How to show fractions in SQL/PostgreSQL?


I'd like to show fractions in PostgreSQL exactly as they are entered (as in no simplification or no turning into decimals). I do not have a preference on what kind of data type is used. I've tried variations of the following but to no avail:

SELECT to_char(2/32, '99/99') AS "Fraction";

This gives a result of " / 0", but what I would like is "2/32".

In my actual situation the numerator and denominator are table field values, like '# enrolled people' / 'total # people'.

Any help is appreciated! Thank you.


Solution

  • Since the numerator and denominator are table columns then use:

    select concat(numerator::text,'/',denominator::text) as "Fraction"