Question: I want to get the result of this query but I'm getting the following error

> 
> Arithmetic overflow error converting expression to data type int
> 
> 
> 

```
SELECT d.name,
ROUND(SUM(mf.size) * 8 / 1024, 0) Size_MBs
FROM sys.master_files mf
INNER JOIN sys.databases d ON d.database_id = mf.database_id
WHERE d.database_id > 4 
GROUP BY d.name
ORDER BY d.name

```

Answer: 
Just replace `8` by `8.0` to cast result to numeric datatype. The error is because
`size` column is `int` and `8` is also `int` so result of multiplication is `int`, but as `size` is close to `int type` upper bound 2147483647 you are overflowing the type by multiplication.

```
SELECT d.name,
ROUND(SUM(mf.size) * 8.0 / 1024, 0) Size_MBs
FROM sys.master_files mf
INNER JOIN sys.databases d ON d.database_id = mf.database_id
WHERE d.database_id > 4 
GROUP BY d.name
ORDER BY d.name

```

You can check it. This will throw exception:

```
DECLARE @i INT = 2147483647
SELECT @i + 1

```

This works:

```
DECLARE @i INT = 2147483647
SELECT @i + 1.0

```

You can read about type precedence <https://msdn.microsoft.com/en-us/library/ms190309.aspx>:

> 
> When an operator combines two expressions of different data types, the
>  rules for data type precedence specify that the data type with the
>  lower precedence is converted to the data type with the higher
>  precedence.
> 
> 
>
Try this:

```
            SELECT d.name,
        ROUND(SUM(CAST(mf.size  as bigint)) * 8 / 1024, 0) Size_MBs
        FROM sys.master_files mf
        INNER JOIN sys.databases d ON d.database_id = mf.database_id
        WHERE d.database_id > 4 
        GROUP BY d.name
        ORDER BY d.name

```