Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a float into int using the 'round' method in SQL Server?

I tried with

select ROUND(1235.53) --(It can contain "n" digit of scale)

But I got this error:

The round function requires 2 to 3 arguments.

I am not sure what is the use of other parameters.

like image 447
Kumawat Avatar asked Sep 20 '16 14:09

Kumawat


People also ask

Can we convert float to int in SQL?

Convert Float to Int In this example, we will convert a float data type to integer. In the following query, we will declare a variable that data type is float and then we will use the SQL CONVERT function in order to convert float value to integer so for that we will perform data converting operation.

How do you ROUND decimal to integer in SQL Server?

SQL Server ROUND() Function The ROUND() function rounds a number to a specified number of decimal places. Tip: Also look at the FLOOR() and CEILING() functions.

How do you convert a float to an integer?

A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function.


2 Answers

Better to use CAST INT/CEILING/FLOOR:

SELECT CEILING(1235.53)
SELECT FLOOR(1235.53)
SELECT CAST(1235.53 AS INT)

CEILING: Gives you the upper bound integer value

Enter image description here

FLOOR: Gives you the lower bound integer value

Enter image description here

like image 107
Sandip - Frontend Developer Avatar answered Oct 15 '22 11:10

Sandip - Frontend Developer


Set decimals to zero

select cast(ROUND(1235.53,0) as int)  Returns 1236

select cast(1235.53 as int)           Returns 1235
like image 29
John Cappelletti Avatar answered Oct 15 '22 10:10

John Cappelletti