Cast syntax to convert a sum to float

SqlPostgresqlCastingType ConversionPostgresql 9.3

Sql Problem Overview


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

Sql Solutions


Solution 1 - Sql

I use the shorthand cast syntax almost everywhere:

SELECT sum(seconds)::float / -1323 AS averag;

More details:

Solution 2 - Sql

You need to use the cast syntax:

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

Solution 3 - Sql

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMAKView Question on Stackoverflow
Solution 1 - SqlErwin BrandstetterView Answer on Stackoverflow
Solution 2 - SqlMureinikView Answer on Stackoverflow
Solution 3 - SqlSouvikView Answer on Stackoverflow