Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Currency Format Expand / Collapse
Author
Message
Posted Friday, September 4, 2009 2:53 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 23, 2011 10:32 AM
Points: 18, Visits: 78
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

Post #783197
Posted Saturday, September 5, 2009 10:27 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:17 AM
Points: 36,944, Visits: 31,448
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #783356
Posted Wednesday, September 9, 2009 2:48 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 23, 2011 10:32 AM
Points: 18, Visits: 78
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
Post #785361
Posted Wednesday, September 9, 2009 8:04 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:17 AM
Points: 36,944, Visits: 31,448
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #785450
Posted Thursday, September 10, 2009 7:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, January 14, 2010 9:09 AM
Points: 141, Visits: 187
Hi, YumiPhx.
You can have a try of RAQ Report. 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
Post #785666
Posted Thursday, September 10, 2009 8:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 23, 2011 10:32 AM
Points: 18, Visits: 78
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
Post #785729
Posted Thursday, September 10, 2009 9:15 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 23, 2011 10:32 AM
Points: 18, Visits: 78
Hi Becklery,
Very good source.
Thank you,
Yumi
Post #785780
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse