Query
Select 9/5 ‘Showing output as 1
I want to get the exact ouput for 9/5
Expected Ouput
1.8 instead of 1
How to make a query
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is it is doing integer math. You can explicitly treat them as decimal numbers by adding
.0:You can also use CONVERT or CAST to be even more explicit:
Note: If either of the operands are decimal numbers, the result will be a decimal number, so you technically only have to convert one of them. So any of these will also work:
The reason it works this was is because the ANSI/ISO-SQL standard actually says that the results of mathematical operations must be the same data type as one of the operands. So if both of the operands are integers, the result according to the ANSI/ISO-SQL standard must also be an integer.
MySQL violates the standard here (presumably in the interest of practicality), but MS SQL complies with it.