ltrim rtrim Causing Floating Field to Round up??

  • 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

  • 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
  • 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,

  • 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
  • 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

  • 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