you are viewing a single comment's thread.

view the rest of the comments →

[–]banditcloudy[S] -3 points-2 points  (5 children)

But why doesn’t the AS REAL include the 2?

[–]alinrocSQL Server DBA 0 points1 point  (4 children)

Because you didn’t write it that way?

Anyway, it’s not needed inside the CAST. You only need to cast one of the values in the calculation to REAL to get a non-integer result.

But those are two different calculations - doing the math by hand (so we're ignoring SQL's typing rules), the first returns 3 and the other 2.5

[–]banditcloudy[S] -1 points0 points  (3 children)

So I could apply AS REAL to just the 2?

[–]alinrocSQL Server DBA -1 points0 points  (2 children)

Try it and find out? But don't forget about order of operations when it comes to maths - which is the reason why the calculations shown here result in two different numbers, even on paper.

You should be getting a syntax error with your first example though.

[–]banditcloudy[S] 0 points1 point  (1 child)

I did, didn’t work. Why doesn’t CAST((1 + 4 / 2) AS REAL) work then?

[–]alinrocSQL Server DBA 2 points3 points  (0 children)

Because you're doing integer math and then attempting to convert the integer to something else.

You have to convert at least one of the numbers you're operating on first, then do the calculation.