[英]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.我在数据库中有一个字符串列,它存储以下值:5/4、3/9、4/3 等。我需要 select 在查询中将此列值显示为数字/十进制。
On using::float operator or to_number() function on the column, it gives an error: invalid input syntax for type double precision: "5/4"在列上使用::float 运算符或 to_number() function 时,出现错误:双精度类型的输入语法无效:“5/4”
I have to do it with a single select statement without use of any procedures or custom functions.我必须使用单个 select 语句来完成,而不使用任何过程或自定义函数。 Any Helps regarding this?对此有任何帮助吗?
You need 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.