SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ltrim rtrim Causing Floating Field to Round up??


ltrim rtrim Causing Floating Field to Round up??

Author
Message
david.ostrander
david.ostrander
SSC-Addicted
SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)

Group: General Forum Members
Points: 404 Visits: 692
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
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42107 Visits: 19829
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.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
david.ostrander
david.ostrander
SSC-Addicted
SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)SSC-Addicted (404 reputation)

Group: General Forum Members
Points: 404 Visits: 692
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,
GilaMonster
GilaMonster
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225409 Visits: 46321
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14533 Visits: 11848
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

parulprabu
parulprabu
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1604 Visits: 136
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search