Cast syntax to convert a sum to float

不羁岁月 提交于 2019-12-04 15:00:01

问题


Using PostgreSQL 9.3, I want to convert the calculated values to data type float.

My first attempt:

SELECT float(SUM(Seconds))/-1323 AS Averag;

Gives me this error:

syntax error at or near "SUM"

My second attempt:

SELECT to_float(SUM(Seconds))/-1323 AS Averag;

Gives me this error:

 function to_float(bigint) does not exist

回答1:


You need to use the cast syntax:

SELECT CAST (SUM(Seconds) AS FLOAT)/-1323 AS Averag;



回答2:


There is also the shorthand cast syntax:

SELECT sum(seconds)::float / -1323 AS averag;
  • Postgres data type cast



回答3:


It is not exact casting but a trick to do the job :) and works almost in any language.

SELECT SUM(Seconds)/-1323.0 AS Averag;

OR

SELECT SUM(Seconds)*1.0/-1323 AS Averag;



来源:https://stackoverflow.com/questions/28736227/cast-syntax-to-convert-a-sum-to-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!