Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a fraction stored as String as decimal value in Postgresql

I have a String column in Database which stores values like: 5/4 , 3/9, 4/3 etc. I have a requirement to select this column value in a query and display it as a number/decimal.

On using ::float operator or to_number() function on the column, it gives an error: invalid input syntax for type double precision: "5/4"

I have to do it with a single select statement without use of any procedures or custom functions. Any Helps regarding this?

like image 423
Sarthak Mehra Avatar asked Oct 29 '25 14:10

Sarthak Mehra


1 Answers

You would need to split that string, convert each part and divide:

select col, split_part(col, '/', 1)::numeric / split_part(col, '/', 2)::numeric as res
from mytable
like image 86
GMB Avatar answered Nov 01 '25 10:11

GMB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!