How to get float value when dividing of two integer number in SQL?
How to get float value when dividing of two integer number in SQL? In this article I'm going to explain how to get decimal output.
How to get float value when dividing of two integer number in SQL? In this article I'm going to explain how to get decimal output.
The following is an example which creates a StudentMark table.
CREATE TABLE [dbo].StudentMark(
[ID] [int] NULL,
Colum1 [int] NULL,
Colum2 [int] NULL,
Colum3 [int] NULL,
Colum4 [int] NULL
)
The sql Insert statement is used to add new records to a table in the database.
Insert into StudentMark values (200,10,20,30.5,40.5)
Insert into StudentMark values (201,11,21,31.5,40.5)
Insert into StudentMark values (202,15.5,21.5,31.5,40.5)
select SUM of three columns divided by fourth columns.
Select SUM(colum1)+SUM(colum2)+SUM(colum3)/ SUM(colum4)from StudentMark group by [ID]
Output :-
30
32
36
This result is incorrect. if want with decimal then use cast or convert the numerator to solve this problem
Example of using Cast:-
SELECT CAST((SUM(colum1)+SUM(colum2)+SUM(colum3))AS FLOAT)/SUM(colum4) AS Result
FROM StudentMark
GROUP BY ID
Output:-
Result
1.5
1.575
1.675
Example of using Convert :-
SELECT CONVERT(FLOAT,(SUM(colum1)+SUM(colum2)+SUM(colum3)))/SUM(colum4) AS Result
FROM StudentMark
GROUP BY ID