October 22, 2013 at 1:34 pm
Hello -
I have a Select query that pulls data from a view. In my Select query I have the line
ltrim(rtrim([Revenue])) AS 'Revenue'
I'm finding out that if I remove the ltrim(rtrim the revenue number comes up correct as 10041.25 but with the ltrim(rtrim back in it gets rounded up to 10041.30.
I didn't think this would do this. Is there a way to fix this?
Regards,
David
October 22, 2013 at 1:37 pm
LTRIM and RTRIM won't chage values of a character string. The problem might be an implicit conversion if your Revenue column is not a character type. To avoid this problem, you should do an explicit conversion.
October 22, 2013 at 1:51 pm
Thank you for the fast response. You stated
To avoid this problem, you should do an explicit conversion.
My column is Revenue (float, null)
How would I do the explicit conversion? On the view or select statment?
Regards,
October 22, 2013 at 2:09 pm
Why are you trimming a float in the first place?
A float cannot have leading or trailing spaces as it's a numeric data type. Only strings can have leading or trailing spaces.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 22, 2013 at 3:46 pm
Casting the float to a VARCHAR value is what causes the rounding you are seeing:
select
a.MyFloat,
ToVarchar = convert(varchar(30),a.MyFloat),
ToLtrimRtrim =ltrim(rtrim(a.MyFloat))
from
( Select MyFloat = convert(float,10041.25E00) ) a
Results:
MyFloat ToVarchar ToLtrimRtrim
---------------------- ------------------------------ -----------------------
10041.25 10041.3 10041.3
October 23, 2013 at 12:00 am
GilaMonster (10/22/2013)
Why are you trimming a float in the first place?A float cannot have leading or trailing spaces as it's a numeric data type. Only strings can have leading or trailing spaces.
Hi David,
What are you trying to do with trim function on FLOAT value?
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply