Currency Format

  • Hi all,

    Do you know how to cut off the decimal for the Currency format without ROUNDING up the number?

    example: $4,816,220.65

    I want the number show on report: $4,816,220 NOT $4,816,221

    your help is much appreciated,

    Yumi

  • YumiPhx (9/4/2009)


    Hi all,

    Do you know how to cut off the decimal for the Currency format without ROUNDING up the number?

    example: $4,816,220.65

    I want the number show on report: $4,816,220 NOT $4,816,221

    your help is much appreciated,

    Yumi

    First of all, you should never store formatted data in the database and you usually shouldn't format data in SQL Server. It's much better for a dozen reasons to do the formatting in the GUI.

    Also, without knowing the underlying datatype of the data you want to convert, I can only guess that you have the data stored as VARCHAR (again, that's a VERY bad thing to do). Here's how to preserve the formatting you requested on a VARCHAR...

    --===== Create a data sample... this is not a part of the solution

    DECLARE @SomeFormattedColumn VARCHAR(20)

    SET @SomeFormattedColumn = '$4,816,220.65'

    --===== Solve the problem

    SELECT LEFT(@SomeFormattedColumn, LEN(@SomeFormattedColumn)-3)

    Of course, that will work correctly ONLY if all your data has a decimal point and two decimal places. You could use this as a bit of a guarantee...

    --===== Create a data sample... this is not a part of the solution

    DECLARE @SomeFormattedColumn VARCHAR(20)

    SET @SomeFormattedColumn = '$4,816,220.65'

    --===== Solve the problem

    SELECT LEFT(@SomeFormattedColumn,

    LEN(@SomeFormattedColumn)

    - ISNULL(LEN(@SomeFormattedColumn)

    - NULLIF(CHARINDEX('.',@SomeFormattedColumn),0)+1

    ,0)

    )

    The numeric functions in that will be faster than doing multiple CONVERTs or CASTS or even using REVERSE.

    ... and now you see why you shouldn't store or create formatted data in SQL Server. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Hi Jeff,

    Thank you so much for your reply.

    The fields that selected for the report are from SQL server BD,

    define as (Float, null). When I set the Currency format in RS as C

    it give me 2 decimals. If I set C0, it will remove the decimal but round it up automatically.

    I tried to use your suggestion but somehow I am having problem with Charindex for those number greater than 7 digits...No clue why.

    Finally, I used FLOOR function and it works fine.

    Select Floor(floatNumberColumn)

    from Table1

    That give me the number without decimal without round it. Then I use the C0 format for report.

    Now I have another problem. Yesterday, I set up Visual Studio Team Foundation Server and now I can not print my report in Color any more

    Report only come black and white. Do you hear this problem before?

    Thank you very much for your help,

    Yumi

  • Ah dang... thanks for the feedback, Yumi. I've gotta remember to read the name of the forum I'm trying to answer the question on. I was thinking pure T-SQL solution which isn't appropriate for this RS case. You did it the right way... letting the service do the formatting after you've nailed the correct value.

    I don't use reporting services enough to know why your reports aren't coming out in color, niow. I'd be tempted to just say "check the Windows printer settings" but I'm just not sure.

    Hopefully, someone can help with that.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Hi, YumiPhx.

    You can have a try of RAQ Report[/url]. As I know, it provides a lot of useful string functions for developers as shown below:

    RAQ Report: Web-based Excel-like Java reporting tool[/url]

  • Jeff, Thanks for quick reply. Don't worry about confusing the forum name. I am new in Microsoft world, so you code is very helpful for me. I will apply Charindex and Nullif function next time.

    Thanks again,

    Yumi

  • Hi Becklery,

    Very good source.

    Thank you,

    Yumi

Viewing 7 posts - 1 through 6 (of 6 total)

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