Select does not return broken value

1

I have the following command:

 SELECT CAST(450/4 AS DECIMAL(15,2))

Return : 112 where the right one would be 112.5

Why this?

NOTE: My SCRIPT is in a procedure, but roughly my problem is in this select

    
asked by anonymous 17.03.2018 / 21:42

1 answer

1

According to the documentation:

  

/ (Division) (Transact-SQL)
link

The division return type will be the highest precedence type (

Make the division result of type decimal :

SELECT CAST(450.0/4.0 AS DECIMAL(15,2))

When writing a numeric constant with decimal point, SQL Server will understand that number is of type decimal :

  

Constants (Transact-SQL)
link

Since decimal has precedence higher than int , I believe that only one of the constants could be of type decimal so that the return is of type decimal also, so this way:

450/4.0

or that:

450.0/4

It should also work.

    
18.03.2018 / 00:53