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

How to remove trailing zeros and decimal? Expand / Collapse
Author
Message
Posted Friday, November 16, 2012 11:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, December 28, 2012 8:35 AM
Points: 9, Visits: 44
Hi, this is my first post here. I tried searching for this and didn't have luck, so I apologize if this has been asked before (perhaps I'm searching the wrong terms as I'm a noob).

I'm trying to edit an SSRS report in Visual Studio '08. It's basically a list of opportunities grouped in columns by sales stage. Below the list are a few fields that sum the estimated revenue subtotals and totals. The value expressions are pretty basic in that it just pulls the opportunity name and appends the revenue in parenthesis and adds the letter M for million. So from the db it'll pull "ABC Company" and "1500000" and show "ABC Company (1.50M)".

I would like to trim the trailing zeros and/or the decimal point when needed. For example, 1.30 should be 1.3, 6.00 should be 6, 0.70 should be 0.7, and 0.00 just 0 by itself.

Below are the expressions in one column I'm working with and I was hoping somone could explain what code to insert and how it fits into my existing expressions. Thanks in advance!

=Fields!name.Value + " (" + FormatNumber(CStr(CDbl(Fields!new_revenue_baseValue.Value)/1000000),2) + "M) " + IIf(Parameters!Ownership.Value = "True"," [" + Fields!ownerid.Value + "]", "")

=(Sum(Fields!stage1.Value, "New")) / 1000000

=(Sum(Fields!stage1.Value, "Existing")) / 1000000

=(Sum(Fields!stage1.Value,"Existing") + Sum(Fields!stage1.Value,"New"))/1000000

Post #1385801
Posted Saturday, November 17, 2012 9:24 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 7:42 AM
Points: 36,954, Visits: 31,463
I don't even know how to spell "SSRS" but, if I were to do something like this in T-SQL, here's one way to do it IF there were always going to be just 2 decimal places. Hopefully, you can connvert it to the language of SSRS.
--===== Create some test data.  This is NOT a part of the solution.
DECLARE @SomeTable TABLE (SomeDecimal DECIMAL(9,2));
INSERT INTO @SomeTable (SomeDecimal)
SELECT 1.50 UNION ALL
SELECT 6.00 UNION ALL
SELECT 0.70 UNION ALL
SELECT 0.03 UNION ALL
SELECT 100.01 UNION ALL
SELECT 0.00
;
--===== This could be turned into an Inline Table Valued Function for ease of use.
WITH
cteSplit AS
(
SELECT IntPart = CAST(CAST(SomeDecimal AS INT) AS VARCHAR(30)),
DecPart = RIGHT(SomeDecimal % 1, 2)
FROM @SomeTable
)
SELECT IntPart
+ ISNULL('.' + NULLIF((LEFT(DecPart,1) + ISNULL(NULLIF(RIGHT(DecPart,1),'0'),'')),'0'),'')
FROM cteSplit
;

As a bit of a sidebar, this is an unusual report display requirement. Why do they want it this way?


--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 #1385949
Posted Tuesday, November 20, 2012 7:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, December 28, 2012 8:35 AM
Points: 9, Visits: 44
Thanks for your reply. They want it this way to save space. Right now, it rounds off to 2 decimal places--so why have "Opportunity (2.00M)" when you can have "Opportunity (2M)". The three saved characters help keep the text on one line since the columns are side by side. Hence, by the time all opportunities are listed, the hope is that they fit on one page when it's exported as a PDF, word, etc.

Is there a way to accomplish this through a value expression in VB.net? I'm not familiar with TSQL, nor would I know where to start entering it. :/
Post #1386920
Posted Tuesday, November 20, 2012 8:43 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 7:42 AM
Points: 36,954, Visits: 31,463
jchicky (11/20/2012)
Thanks for your reply. They want it this way to save space. Right now, it rounds off to 2 decimal places--so why have "Opportunity (2.00M)" when you can have "Opportunity (2M)". The three saved characters help keep the text on one line since the columns are side by side. Hence, by the time all opportunities are listed, the hope is that they fit on one page when it's exported as a PDF, word, etc.

Is there a way to accomplish this through a value expression in VB.net? I'm not familiar with TSQL, nor would I know where to start entering it. :/


I'm sure there is but I'm not a VB guy. Hopefully this post will server as a "bump" to entice one to take a shot at it.


--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 #1386966
Posted Tuesday, November 20, 2012 2:21 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 12:03 PM
Points: 1,480, Visits: 1,028
Here is what I used to populate my DataSet
SELECT 'ABC' AS Company, 1000000 AS Revenue UNION ALL
SELECT 'DEF' AS Company, 1550000 AS Revenue UNION ALL
SELECT 'GHI' AS Company, 2100000 AS Revenue UNION ALL
SELECT 'JKL' AS Company, 2010000 AS Revenue

Here is the Expression I Used
=Fields!Company.Value + " (" + CStr(SUM(Fields!Revenue.Value)/1000000) + "M)"
and It Does exactly what your asking.
By Using the Function FormatNumber(<<Expr>>,2) Your telling it to force the Number to be 2 Decimal places.
If you CStr a Number it automatically removes the trailing zeros.
if you don't trust it, a more explicit removal is in order I suppose.

But this looks way worse.
="(" + iif(CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)).EndsWith("00"),Left(CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)),Len(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2))-3),iif(CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)).EndsWith("0"),Left(CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)),Len(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2))-1),CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)))) + "M)"
Post #1387159
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse