A lot of answers here say use
SELECT @value - FLOOR(@value)
to get the decimal part of a number. See here or here for examples.
I'm getting what I consider to be weird behavior when I do this.
DECLARE @test DECIMAL(38, 8)
SET @test = 123.05468800
SELECT @test - FLOOR(@test)
Result: 0
I can do
SELECT 123.05468800 - FLOOR(123.05468800)
Result: 0.05468800
and I can change the type of @test to FLOAT and it works. Also FLOOR(@test) returns 123 as expected.
I imagine this has something to with precision of the decimal type, but the only MSDN page I could find on the subject is really sparse.
So what's going on? Why don't I get the decimal portion .05468800? And what should I be doing or using to get this?
Note the middle 2 for gbntest are
decimal (38,0)However, with constants or
decimal (28,8)it works. So does(29,8)and(30,8)But then with
(31,8)you get(38,7)back.The MSDN "Precision, Scale, and Length" describes why
For (31,8), you get a precision of (40, 8)
(40,8)should be adjusted down to(38,6). So I've bollixed my calculation somewhere :-) but I hope you get the idea...