Skip to main content
January 17, 2012
Answered

CF Not Returning Correct Results

  • January 17, 2012
  • 3 replies
  • 1046 views

I have a column that has both positive and negative numbers and I am trying to seperate those numbers into a seperate column each. My code below is working if each barcode has both a positive and negative number to compare, but errors are thrown such as "can't convert " to a numeric value" if I just have all positive numbers in the colum. Which, in some casees, I will have because I am importing a CSV file. Can anyone help with the code? - Thanks.

<!--- SUM POSITVE QUANTITY --->
<cfquery name="SumPositiveTemp" datasource='inventory'>
SELECT barcode, sum(quantity) as quantitysumPositive
FROM temp
WHERE Quantity > 0 
GROUP BY barcode
</cfquery>

<!--- SUM NEGATIVE QUANTITY --->
    <cfquery name="SumNegativeTemp" datasource='inventory'>
    SELECT barcode, sum(quantity) as quantitysumNegative
    FROM temp
    WHERE Quantity < 0 
    GROUP BY barcode
    </cfquery>
   
<!--- SUBTRACT QUANTITY ISSUED MINUS QUANTITY RECEIVED TO GET QUANTITY SOLD --->
    <cfset QuanitySold = '#SumPositiveTemp.quantitysumPositve-SumNegativeTempquantitysumNegative#'>

<!--- INSERT POSITVIE/NEGATIVE NUMBERS INTO TABLE--->
<cfoutput query="SumPositiveTemp">
    <cfquery name="InsertPositiveIntoCart" datasource="inventory">
   INSERT INTO Cart1 (barcode, QuantityOut, QuantityIn, QuantitySold)
    VALUES (#SumPositiveTemp.barcode#, #SumPositiveTemp.quantitysumPositive#, #SumNegativeTemp.quantitysumNegative#, #QuantitySold#)
    </cfquery>
</cfoutput>

    This topic has been closed for replies.
    Correct answer

    Yes, I found that when the barcode did not have matching POS and NEG numbers it would return a null value. After more research, I found that <CFIF ISNUMERIC(NEG VALUE)> was the solution to avoiding a null value and use <CFSET NEG VALUE = 0> in that situation.

    3 replies

    Correct answer
    January 17, 2012

    Yes, I found that when the barcode did not have matching POS and NEG numbers it would return a null value. After more research, I found that <CFIF ISNUMERIC(NEG VALUE)> was the solution to avoiding a null value and use <CFSET NEG VALUE = 0> in that situation.

    BKBK
    Community Expert
    Community Expert
    January 18, 2012

    geraldselectric225 wrote:

    Yes, I found that when the barcode did not have matching POS and NEG numbers it would return a null value. After more research, I found that <CFIF ISNUMERIC(NEG VALUE)> was the solution to avoiding a null value and use <CFSET NEG VALUE = 0> in that situation.

    What you have is good. I think you could improve it with this kind of logic:

    <cfif NOT isNumeric(someVar) or someVar LTE 0>

        <cfset someVar = 0>

    </cfif>

    Inspiring
    January 17, 2012

    Sounds like one of your select queries is returning null for a quantity.

    Tayyab Hussain
    Inspiring
    January 17, 2012

    Can you send me a small database (Access would work) So that i can test the code?