Advertisement
sunraycafe

SQL Division

Dec 1st, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.92 KB | None | 0 0
  1. /*  SQL Division
  2.  
  3. A coworker and I were trying to run some statistics this week and found something we thought
  4. interesting. Some of you may already know this, but we didn't.
  5.  
  6. When performing division, if both divisor and dividend are whole numbers, the result will be
  7. a whole number, even if the answer would be a decimal. However, if either the divisor or
  8. dividend are a decimal, the result will also be a decimal.
  9. */
  10.  
  11. print 15/10
  12. print 15/10.0
  13. print 15*.1
  14. print 15*.10
  15.  
  16. /* Results
  17. 1
  18. 1.500000
  19. 1.5
  20. 1.50
  21. */
  22.  
  23. /*
  24. This behavior was particularly confusing when we tried to cast the initial problem as
  25. a decimal, but it still left off the fractional portion. This was because the answer
  26. was determined before it did the cast.
  27. */
  28.  
  29. print cast(15/10 as decimal(10,2))
  30. print cast(15/10.0 as decimal(10,2))
  31. print cast(15*.1 as decimal(10,2))
  32. print cast(15*.10 as decimal(10,2))
  33.  
  34. /* Results
  35. 1.00
  36. 1.50
  37. 1.50
  38. 1.50
  39. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement