SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to remove trailing zeros and decimal?


How to remove trailing zeros and decimal?

Author
Message
jchicky
jchicky
SSC Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 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


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)

Group: General Forum Members
Points: 204376 Visits: 41950
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
jchicky
jchicky
SSC Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 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. :/
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)SSC Guru (204K reputation)

Group: General Forum Members
Points: 204376 Visits: 41950
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Ray M
Ray M
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4831 Visits: 1076
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)"
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search