Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast syntax to convert a sum to float

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 
like image 851
MAK Avatar asked Feb 26 '15 06:02

MAK


People also ask

How do you typecast into float?

int total=0, number=0; float percentage=0.0; percentage=((float)number/total)*100; printf("%. 2f", percentage); Add (float) before the variable name when you use.

Can we type cast int to float?

Use the Typecasting Method to Convert an Integer Into a Float in Java. Typecasting is a method in which you convert one type of data type into some other type explicitly. By using this technique, we can convert an integer value into a float value. To typecast an integer value to float, we will use the float keyword.

What is CAST () and convert () functions in SQL Server?

The cast and convert functions provide similar functionality. They are used to convert a value from one data type to another. So let's take a look at a practical example. The example is developed in SQL Server 2012 using the SQL Server Management Studio.

Can we convert int to float in SQL?

In this case the order of precedence is in your favour, and you get a float on both sides, and a float as a result of the + . But SUM(aFloatField) + 0 does not yield an INT, because the 0 is being implicitly cast to a FLOAT. I find that in most programming cases, it is much preferable to be explicit.


1 Answers

I use the shorthand cast syntax almost everywhere:

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

More details:

  • Postgres data type cast
like image 107
Erwin Brandstetter Avatar answered Oct 15 '22 02:10

Erwin Brandstetter