SQL Server: round decimal number and convert to int (within Select)
You can use ROUND
function to round the value to integer:
ROUND(INT, 100 * AVG(CASE WHEN col2 = col3 THEN 1.0 ELSE 0.0 END), 0) AS matchPercent
This will retain the type, e.g rounded float
will stay float
. If you also need to return int
data type (or other integer data types), you need to also convert it:
CONVERT(INT, ROUND(INT, 100 * AVG(CASE WHEN col2 = col3 THEN 1.0 ELSE 0.0 END), 0)) AS matchPercent
Use the round
function to round the number:
ROUND(100 * AVG(CASE WHEN col2 = col3 THEN 1.0 ELSE 0.0 END), 0) AS matchPercent
If you want to round the number first and then convert it to an integer, you also can add 0.5 to the number that you want to convert to an integer.