Trying to limit the fidelity (to 2 decimal places) of a calculated value?

Hi Everyone,
I have a column that shows the Profit / Loss % for items sold.
ISNULL(((T0.LineTotal - T0.StockValue) / NULLIF(T0.StockValue, 0)) * 100, 0
Because it is possible for StockValue to be a zero (0) amount in our system it was necessary to add the NULLIF function.
If a NULL value is detected then a value of 0 is ultimately returned. Due to the fact that there could still be a profit or a loss in this column I needed to add 'N/A', which I achieved by casting the calculation to a varchar and then applying a CASE statement, the full syntax is as follows -
CAST(ISNULL(((T0.LineTotal - T0.StockValue) / NULLIF(T0.StockValue, 0)) * 100, 0) AS varchar) = '0.00000000000000' THEN 'N/A'
ELSE CAST(ISNULL(((T0.LineTotal - T0.StockValue) / NULLIF(T0.StockValue, 0)) * 100, 0) AS varchar) END
AS 'Profit / Loss %'
My challenge is that the % for profit has an excessive level of fidelity, it currently goes to 14 decimal places (e.g.: 134.43223443223443)!
I would like to limit the fidelity to 2 decimal places (e.g: 134.43). I have attempted to do this by 'double casting', first casting to a decimal then to a varchar (to allow for the 'N/A') -
CAST(CAST(ISNULL(((T0.LineTotal - T0.StockValue) / NULLIF(T0.StockValue, 0)) * 100, 0) AS decimal) AS varchar)
In this case the fidelity is not sufficient, and I lose all decimal places. If I stipulate the nature of the decimal place, e.g.: decimal(4,2), then I get an "arithmetic overflow" error.
How can I make my results either 'N/A' or a numeric result (with 2 decimal places)?
Any help will be greatly appreciated.
Kind Regards,
David

Hi David...
Check this
*    Purpose: Lists the Sales History of Items by (user designated): 
*    Item Code (range) and / or    
*    Whs Code (range) and / or 
*    Industry Code and / or    
*    BP (Customer Code) 
DECLARE @begItemCode nvarchar(20), @endItemCode nvarchar(20), @whsCode nvarchar(5), @indCode nvarchar(5), @custNo nvarchar(10) 
SET @begItemCode = '' 
    IF @begItemCode = '' 
        SET @begItemCode = '00%' 
SET @endItemCode = '' + 'Z' 
    IF @endItemCode = 'Z' 
        SET @endItemCode = 'ZZ%' 
SET @whsCode = '' 
    IF @whsCode = '' 
        SET @whsCode = '%' 
SET @indCode = '' 
    IF @indCode = '' 
        SET @indCode = '%' 
SET @custNo = 'C000002' 
    IF @custNo = '' 
        SET @custNo = '%' 
        select tt.[Document Date],tt.[Document No.],tt.[Doc Type],tt.[Cust Code],tt.[Customer Name],tt.[Item Code],tt.[Item Description] ,
        tt.whscode, tt.[Line Net],tt.[Line Cost],tt.[Itm Avg Cost] , tt.[Profit / Loss],
        cast(round(tt.[Profit / Loss %],2,0) as decimal(18,2) ) as 'PL%',tt.[Vendor Name],tt.[Salesman Name] from (
SELECT 
T1.DocDate AS 'Document Date' 
, T1.DocNum AS 'Document No.' 
, 'Invoice' AS 'Doc Type' 
, T1.CardCode AS 'Cust Code' 
, T1.CardName AS 'Customer Name' 
, T0.ItemCode AS 'Item Code' 
, T0.Dscription AS 'Item Description' 
, T0.Quantity AS 'Ship Qty' 
, T0.WhsCode 
, T0.LineTotal AS 'Line Net' 
, T0.StockValue AS 'Line Cost' 
, T2.AvgPrice AS 'Itm Avg Cost' 
, (T0.LineTotal - T0.StockValue) AS 'Profit / Loss' 
, CASE WHEN 
--ROUND(
  CAST(ISNULL(((T0.LineTotal - T0.StockValue) / NULLIF(T0.StockValue, 0)) * 100, 0) AS varchar) = '0.00000000000000' THEN 'N/A' 
  ELSE CAST(ISNULL(((T0.LineTotal - T0.StockValue) / NULLIF(T0.StockValue, 0)) * 100, 0) AS varchar) END    
  AS 'Profit / Loss %' 
, T4.CardName AS 'Vendor Name' 
, T5.SlpName AS 'Salesman Name' 
FROM  dbo.INV1 T0 
INNER JOIN  dbo.OINV T1 ON T1.DocEntry = T0.DocEntry 
INNER JOIN  dbo.OITM T2 ON T2.ItemCode = T0.ItemCode 
INNER JOIN  dbo.ITM1 T3 ON T3.ItemCode = T0.ItemCode AND T3.PriceList = 1 
INNER JOIN  DBO.OCRD T4 ON T4.CardCode = T2.CardCode 
INNER JOIN  DBO.OSLP T5 ON T5.SlpCode = T1.SlpCode 
WHERE T1.DocType = 'I' AND T2.OnHand > 0 AND T0.ItemCode >= @begItemCode AND T0.ItemCode <= @endItemCode AND T0.WhsCode LIKE @whsCode 
AND T2.U_SCE_IN_Industry LIKE @indCode AND T1.CardCode LIKE @custNo 
UNION ALL 
SELECT 
T10.DocDate AS 'Document Date' 
, T10.DocNum AS 'Document No.' 
, 'Credit' AS 'Doc Type' 
, T10.CardCode AS 'Cust Code' 
, T10.CardName AS 'Customer Name' 
, T9.ItemCode AS 'Item Code' 
, T9.Dscription AS 'Item Description' 
, -1 * T9.Quantity AS 'Ship Qty' 
, T9.WhsCode 
, -1 * T9.LineTotal AS 'Line Net' 
, -1 * T9.StockValue AS 'Line Cost' 
, T11.AvgPrice AS 'Itm Avg Cost' 
, -1 * (T9.LineTotal - T9.StockValue) AS 'Profit / Loss' 
, CASE WHEN 
  CAST(ISNULL(((T9.LineTotal - T9.StockValue) / NULLIF(T9.StockValue, 0)) * -100, 0) AS varchar) = '0.00000000000000' THEN 'N/A' 
  --ELSE CAST(CAST(ISNULL(((T9.LineTotal - T9.StockValue) / NULLIF(T9.StockValue, 0)) * -100, 0) AS decimal) AS varchar) END 
  ELSE CAST(ISNULL(((T9.LineTotal - T9.StockValue) / NULLIF(T9.StockValue, 0)) * -100, 0) AS varchar) END 
  AS 'Profit / Loss %' 
, T13.CardName AS 'Vendor Name' 
, T14.SlpName AS 'Salesman Name' 
FROM  dbo.RIN1 T9 
INNER JOIN  dbo.ORIN T10 ON T10.DocEntry = T9.DocEntry 
INNER JOIN  dbo.OITM T11 ON T11.ItemCode = T9.ItemCode 
INNER JOIN  dbo.ITM1 T12 ON T12.ItemCode = T9.ItemCode AND T12.PriceList = 1 
INNER JOIN  DBO.OCRD T13 ON T13.CardCode = T11.CardCode 
INNER JOIN  DBO.OSLP T14 ON T14.SlpCode = T10.SlpCode 
WHERE T10.DocType = 'I' AND T9.ItemCode >= @begItemCode AND T9.ItemCode <= @endItemCode AND T9.WhsCode LIKE @whsCode 
AND T11.U_SCE_IN_Industry LIKE @indCode AND T10.CardCode LIKE @custNo  
  ) as tt
Hope Helpful
Regards
Kennedy

Similar Messages

  • Yielding the desired number of decimal places using the AVG function in t-sql

    Hello again.  Confused retired hobby coder having trouble setting up a scalar-valued function to return the desired number of decimal places from a AVG query.
    Whenever I run the following script I get the number of decimals I desire:
    Using the above I created a scalar-valued function as follows:
    Running this function as: SELECT [dbo].[TestHCIPartial] (1,3)  my return is -7.
    Can you help me with the function causing it to yield the answer to 6 decimal places?
    Thanks and regards, Minuend.

    You've not specified precision and scale in UDF return type. So you're leaving it to server to interpret it based on default settings
    If you want exactly up to four decimal places tweak udf as below
    ALTER FUNCTION..
    RETURNS decimal(20,4)
    AS
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Three decimal places in my calculation

    Hi,
    I have a division calculation, a/b where both a and b are int's.
    now when i want the result upto 3 decimal places. i have done this.
    Double d = a/b;
    DecimalFormat c = new DecimalFormat("0.###");
    c.format(d);
    System.out.println(d);
    Still I am unable to get 3 decimal places. I want 3 decimal places even for a 0 value like 0.000.
    Thanks.

    I have a division calculation, a/b where both a and b are int's.
    now when i want the result upto 3 decimal places. i have done this.
    Double d = a/b;You do know of course that d will be rounded down to the nearest int as you're doing int division here. Also d probably doesn't need to be a Double Object as double will do nicely. Something like so will do:
    double d = (double)a/b; // this will do double division which is what you wantthen format it as you wish.

  • Trying to limit the number of thumbnails or files that generate on startup  - running Elements 9

    I swear I remember being able to limit the number of files that came up on startup to speed up E-9, but can't find it anywhere! 
    Anyone know how to do this?

    Been there done that. Thought there was a way to limit date range of working files, like 90 days.
    Thanks.
    Sent from my iPad, please excuse typos and poor grammar.

  • How to limit the response rows while invoking a stored procedure from OSB 10gR3

    Dear Experts,
    I am trying to limit the response while invoking a stored procedure from OSB via DB adapter.
    Here the stored procedure returns cursors as response. I tried using the MaxTransactionSize propertiy (which is used for polling the database option in DB adapter).
    We are investigating the ways to protect the application from response containing huge number of rows.
    Is there any way to restrict the number of rows returned in each and every cursor while invoking the stored procedure via DB adapter?
    Thanks
    Ram

    Hi James ,
    I want to know how to run stored procedure using jca adapter in OSB11g.
    I am new to stored prcoedure,will be great if you could share some example on it as above.
    Req: Single input xml will be posted on queue ,need to perform 3 DB operation (with same input):
    1.Insert operation to insert master table contents.
    2.select the primary key column value from master table for the last inserted record.
    3.Need to insert primary key column value + other fields frm same xml file to child table.
    Pls assist me on how to create a simple stored procedure and to run the same with OSB 11g.I am familiar with creation of DB adapter using Jdeveloper,we have option to perform insert and to call stored procedure here,will it be useful for this scenario?
    Edited by: Anitha R on Nov 21, 2010 9:30 PM

  • B1 Query returning truncated decimal places when the CASE statement is used

    Hi All,
    Perhaps this is a friday thing.
    In B1 the price setting is for 5 decimal places. I have a query the run a business process looking at the data in the Special Prices Tables. When I run the Query in SQL, the output show the correct number of decimal places. However, when the Query is then  saved and run in B1, the output is truncated to 2 decimal places. Any ideas as to how I can prevent this for happening?
    T0: Points to teh OSPP Table
    T1: Points to the OSP1 Table
    The portion of the query causing the issue is as follows:
    case
       when (T1.price is not null) then
             T1.price
       else
           T0.price
    end

    Hi Earl
    Seems you are right, I have tested with a few different formats and each time get a 2 decimal result. In SQL help I found the following which may explain why:
    Result Types
    Returns the highest precedence type from the set of types in result_expressions and the optional else_result_expression. For more information, see Data Type Precedence (Transact-SQL).
    Even tried it in SQL with a stored procedure storing to a temp table and it shows the full decimals, but executing the SP in SAP Business One results in 2 decimals again.
    This is indeed a strange occurrence and I a not sure how you are going to solve it! You can use NVARCHAR for example except that it right aligns the values returned, but at least it doesn't drop the decimals.
    Kind regards
    Peter Juby

  • How do you limit the number of e-mails that stay on the I pad?

    I am trying to limit the number of e-mails on my I-pad.  I have 498 e-mails.
    Help

    I believe you need to manually delete them one at a time..
    Are the emails from an IMAP account(s)

  • Edit a list  with "button list" template to limit the no: of list entries

    Hi Friends,
    I am having a list with "button list" template. But this list is having a large number of list entries. I am trying
    to limit the list entries shown in a page. For example display first '6' list entries and at the end of the 8th list entry
    having a "Next" or ">" and when pressing it show the next '6' list entries and so on. Is there any way to do this.
    Please help,
    Thanks,
    Tj

    With each line that you are writing out, you will have to add two additional fields, one for line number and another for page number.
    You will move sy-linno and sy-pagno to these fields respectively and modify the record.
    Then you will use the READ LINE itab-linno OF PAGE itab-pagno.
    Srinivas

  • Limit the number of characters in Text Components

    I'm trying to limit the number of characters in text components. So I use the follow KeyListener:
    private class KeyHandler extends KeyAdapter{
    String oldText;
    private KeyHandler(){ }
    public void keyTyped(KeyEvent ev){
    String newText = textField.getText();
    System.out.println("Old text: " + this.oldText); //debug
    System.out.println("New text: " + newText); //debug
    if(newText.length() < limit){
    this.oldText = newText + ev.getKeyChar();
    }else{
    textField.setText(this.oldText);
    if(textField.getCaretPosition() + 1 > limit)
    textField.setCaretPosition(limit - 1);
    But the problem is that after that, the last key is typed in the component and the last key always appear.
    �Does anyone know the way for consume the key presses?
    Thank you

    (Belongs in the Swing forum.)
    The callback method keyTyped comes after keyPressed and keyReleased.
    The event.consume() should consume the event.
    Consider cut/copy/paste.
    Maybe a addPropertyChangeListener would be better?

  • How do I apply filters or limit the rows on my report using a Date field in SQL report builder 3.0?

    I have a status of completed and a date field in the dataset. The date field is either empty or contains a date. All 2015 dates are holding dates.
    So how can I limit the report to only pull completed status with an empty date or a holding date?
    I have not been able to set an OR option on a date field filter and if I add two filters on the date column one for empty one for > 12/31/2014 then it treats it as an "and" and pulls nothing from the list I have in sharepoint.
    any help will be appreciated.

    Hi MB,
    In Reporting Services, the relationship of  filters is “And”, and there is no option to change the relationship from “And” to “Or”. While we can use “or” operator within the expression to create one filter to integrated all filters to work around this
    issue.
    We can add a filter as follows in the dataset to limit the rows in your report:
    Expression: =Fields!date.Value is nothing or Fields!date.Value>"12/31/2014"    Type: Boolean
    Operator: =
    Value: true
    If there are any other questions, please feel free to ask.
    Thanks,
    Katherine Xiong
    If you have any feedback on our support, please click
    here.
    Katherine Xiong
    TechNet Community Support

  • Changing the Decimal places for Percentage in cost distribution in SC

    Hi Gurus,
    Is it possible to change the number decimal places for the Percentage field in cost Assignment tab in Shopping cart in cost distribution option?
    The user wants 3 decimal places, currently we can enter only 2 decimal places.
    I have gone thro' couple of blogs which are talking about changing the price decimal and not the percentage field decimal. (in IMG setting: General settings > Currencies > Set Decimal places)
    So, if anyone come acorss this issue, please let me know.
    Thanks and Regards,
    Nowsath

    Decimal Places Factor of the Receiver Weighting Factors
    Specifies the number of valid decimal places with which the given weighting factors are valuated.
    Example
    You enter 123456 as the weighting factor. Depending on the factor, this number is interpreted as:
    Factor Decimal places Weighting factor
    1 0 123456
    10 1 12345.6
    100 2 1234.56
    1000 3 123.456
    10000 4 12.3456
    100000 5 1.23456
    1000000 6 0.123456
    Please check with your FICO consulant .what settings you have in backend system.
    http://help.sap.com/saphelp_nw04/helpdata/en/bb/bdbdc4575911d189240000e8323d3a/frameset.htm
    Muthu

  • Changing the Decimal places in the currency

    Hi all
    we are using the currecy SAR. Decimal places was set to 4, now that they want to revert back to 2 decimal places. i understand that by doing so they would be inconsistencies in the data allready recorded. how to avoid that?
    is there any programmes / reports that would fix such inconsistencies?
    any OSS notes?
    thanks and regards
    gkk

    HI,
    You should NOT change the decimal places in a productive system.
    If you would do, the correction of the posted documents afterwards would be a remote consulting issue. Which you have to pay for SAP.
    You see, all posting you have done until now, would get displayed in a wrong way, and the new posting wouldn't compare to the others.
    Please check the note 137626 describes this.
    If you really need to change the decimals, you should create a new currency type, for example your current is SAR, and now you should create a SA2. Then you should enter new rations and exchange rates for it, and work with the new one.
    Also if you want to set the decimals to two digits from four, youshould not enter an entry in the OY04(TCURX).
    In this table you can only enter a value, that discriminate from 2 decimals. If there is no entry in the table, it means the currency has two decimals.
    Please assign points if it useful.
    Regards
    Ravinagh Boni

  • 0QUANTITY - Six decimal places - Rouding to the third decimal place

    Hello experts, how are you?
    We are facing a problem with decimal places here in the project.
    We're using M3 unit of measure and the user required 6 decimal places.
    We already changed ANDEC and DECAN fields from T006 table to value 6,
    the key figure itself (0QUANTITY), the key figure in the InfoCube and in the input-ready query, but the recorded data is being rounded to the third decimal place.
    Can someone help us with it?
    Thanks in advance,
    Helder Rios

    Hi,
    First of all I'd like to thank you for your help guys.
    Ashish, I changed the data element RSKYFQUA to 6 decimal places. But when I try to activate the cube, the system is not able to re-create the fact tables.
    Do you know if there's a solution for it?
    Thanks in advance,
    Helder Rios

  • Reduce the field value from three decimal places to two decimal places

    hi experts,
    plz help me with this
    actually i have to display the  value of the field MENGE in the list with two decimal places.
    but when i go and see the field menge in the table mseg it is having three decimal places.
    so could u plz suggest me how to solve this.
    regards
    siri

    Please try using the DEIMALS extension of the WRITE statement.
    data: menge type menge_D value '1234'.
    write:/ menge decimals 2.
    Regards,
    RIch HEilman

  • How to add 2 decimal places if the number is whole number(Integer)

    Hi Gurus- I had a requirement to add the zeros for two decimal places even the number is an whole number.
    Example: I had a column which i gave the datatype as Number(8,2) .While Inserting from the flat file to the custom table column,i am rounding it to 2 decimal places.But if its the whole number i want that also to be rounded to 2 decimal places.
    1) 3456.89675 It's getting inserted as 3456.89
    2) 123 It's getting inserted as 123 (Instead i want this to be inserted as 123.00)
    3) 123.50000 It's getting inserted as 123.5 (Instead i want this to be inserted as 123.50)
    Can you please let me know how can i achieve it to insert any number(decimal or whole number) from the flat file as 2 decimal places in the custom table column.
    Thanks in advance.

    Please do not post duplicates -- To update the date for the first of the month

Maybe you are looking for

  • Transfer Files from Old MacBook to New MacBook Pro

    I'm just about to take delivery of a new macbook pro which will have Lion OS on it. My present MacBook is running 10.6.8 and I have a Time Capsule on my network which contains backups of this MacBook. Is there a procedure to folow which will allow me

  • Rearranging printing presets - is it possible?

    I've managed to make a number of printing presets for various paper sizes and types. Most of them are duplicates of previous settings that I then tweaked and renamed. The problem is, I end up with them "grouped" somewhat randomly because I can't rear

  • International Plan 500 minutes

    How can I check the monthly usage and remaining minutes?

  • IDM_Event_Viewer issue

    Hi all, We have 3 sensors which are integrated in an Event viewer software. Previousy two of the devices were giving "failed to open the connection" error. So i deleted and added the devices manually and successfully added. Now the problem is one of

  • How do I set the default startup sequence for iWork documents under mountain lion?

    I am frustrated when starting Pages under Mountain Lion (OSX 10.8.2). Instead of opening with the documents that I was working on when I closed the app, it opens with a standard "open" dialog box. I am forced to navigate through my folders and files