How to remove right most two zeros in sql server

Posted on 2009-02-24
Last Modified: 2012-05-06
I am rounding a decimal digit(18,6) to (10,4).
The data in the table is like
I have to select the above data in the following format

1) toal length of record should be 10
2) I have to get only '4' digits after decimal point(after rounding)
3) I have to add leading zeros if length is less than 10

I have written the following sql

select RIGHT('0000000000' + replace(round(27.427000,4),'.',''), 10)

can any one help me

Thanks ,
Question by:coventri
    LVL 60

    Accepted Solution

    this should do it:

    declare @x decimal(18,6)
    SET @X = 27.427000
    select right('0000000000000000' + replace(cast(cast(@x as decimal(10,4)) as varchar(10)), '.', ''), 10)
    LVL 39

    Expert Comment

    This will also work.

    declare @x decimal(18,6)
    SET @X = 27.427000
    select right('0000000000000000' + ltrim(str(@x*10000)), 10)
    LVL 35

    Expert Comment

    by:David Todd

    This is the sort of thing that is more easily handled in the presentation layer, rather than in SQL. Most output fields have formats that are pretty flexible. What are you using for presentation? Excel? Crystal Reports? SQL Reports? Other?


    PS I've no doubt that the previous suggestions will work and work well. I'm just asking if this is the best place to do this formatting ...
    LVL 42

    Expert Comment

    it is working too:

    declare @x decimal(18,6)
    SET @X = 27.427000
    select RIGHT('0000000000' + replace(cast(@x as dec(10,4)),'.',''), 10)
    select RIGHT('0000000000' + replace(cast(27.427000 as dec(10,4)),'.',''), 10)

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now