Friday, August 10, 2018

T-SQL and some stupid math

This is driving me nuts!

How does the following not yield 50%?

25/50

Well, unless you're Transact-SQL I suppose it does.  This is probably some well known "feature" that I've missed all these years.

Guess what yields 50%?

25./50

See what I did there?

Unless you include the decimal, or cast one of your operands as a decimal-included type (such as REAL, FLOAT, etc.) you'll get integer truncation.

Do we have that problem in Excel?  No chance.

Oh, make no mistake this is documented.  Does that make it ok?  I suppose it depends on whether you realize it or not when you use it.  What would the average programmer think?  I would suggest .5.  But since you're Microsoft T-SQL and your types are loose then you make the rules.

/rant

No comments:

Post a Comment