0
select 10/2

Output : 5

Select 2/10

Output : 0

Select Convert(numeric(2,2),2/10)

Output : 0.00

Select 10*(Convert(numeric(2,2),2/10))

Output : 0.00

Select 10*(2/10)

Output : 0

Why is the output flawed ?

Are the arithmetic function (Divide) working ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • It's not ***flawed*** - it's doing **exactly** what's documented - you're using **integer division** and `2 / 10` in integer division **is** 0 - no matter what output type you define (this is applied only *after* the operation).... – marc_s Aug 24 '15 at 08:14
  • Thank you. I had no idea about integer division here. Now it seems perfectly fine to me. Thank you again. – Saurabh Yadav Aug 24 '15 at 08:16

1 Answers1

1

When you divide int and int you get int. For this reason you need to divide int and float, hence select 10/2.0.

When you convert to numeric you convert result - so you receive 0.00.

For this reason 2/10 result 0. You must convert before division or add decimal part like .0

There is a similar question.

Community
  • 1
  • 1
Bogdan Bogdanov
  • 1,707
  • 2
  • 20
  • 31