Not round value

  • Hi People,

    I have values like this:

    125,4587878

    1,369888

    15487,9887

    I just need two numbers after ',', but I dont want round the value.

    125,45

    1,36

    15487,98

    Please, help me..

    Thanks

     

  • SELECT CASE WHEN CHARINDEX(',', @value) > 0 THEN LEFT(@value,CHARINDEX(',', @value)+2) ELSE @value END

  • It's recommended to open BOL time to time (press F1)

    C. Use ROUND to truncate

    This example uses two SELECT statements to demonstrate the difference between rounding and truncation. The first statement rounds the result. The second statement truncates the result.

    StatementResult

    SELECT ROUND(150.75, 0)

    151.00

    SELECT ROUND(150.75, 0, 1)

    150.00

    In your case : ROUND(Value, 2, 1)

    _____________
    Code for TallyGenerator

  • G'Day,

    DECLARE @TestString VARCHAR (25),

            @TestNum DECIMAL(20,10)

    SET @TestNum = 125.4587878

    SET @TestString = CAST(@TestNum AS VARCHAR)

    SELECT @TestString, LEFT(@TestString,CHARINDEX('.',@TestString)+2)

    SET @TestNum = 1.369888

    SET @TestString = CAST(@TestNum AS VARCHAR)

    SELECT @TestString, LEFT(@TestString,CHARINDEX('.',@TestString)+2)

    SET @TestNum = 15487.9887

    SET @TestString = CAST(@TestNum AS VARCHAR)

    SELECT @TestString, LEFT(@TestString,CHARINDEX('.',@TestString)+2)

    Hope this helps

    Wayne

  • Thanks a lot 🙂

  • For some reason people prefer to use slow and not reliable (locale dependable - will not work with $12,050.25) solutions and ignore proper ones.

    _____________
    Code for TallyGenerator

  • Why do you think I don't hang out here anymore?!?!

  • Oh no, Remi say it isn't so.

  • Ten years ago I don't think the truncate feature exisited (sysbase).  I gave up looking.  Sergiy, thanks so much for posting.  I wonder how long the truncate feature has been there?

    Thanks again,

    ron

  • Check my recent post count if you need a confirmation .

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply