Click here to Skip to main content
15,908,175 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have sql query i want to sort the data but due to str function it's not working.If i removed str function it gives me error that Error converting data type nvarchar to numeric.
so help me what i have to do.please help
Posted
Comments
hypermellow 23-May-14 7:47am    
Can you post your query so we can have a look?
Member 10685464 23-May-14 7:49am    
IF exists (SELECT * FROM sysobjects WHERE
id=object_id('usp_INV_get_InventoryActivity'))
DROP PROCEDURE usp_INV_get_InventoryActivity
GO

/*==========================================================================================
**
** FILE : usp_INV_get_InventoryActivity.sql
**
** Author :- JP/16-06-2009
** PURPOSE OF FILE :This procedure will INSERT or UPDATE INV_STOCK_TRANSACTION_HISTORY table
for the months WHEN transactions does not happen
**
** This Proc is executed by:
**
Modifications:
07 Feb 2013 : Riddhi R- Added case for OpeningBalance,ClosingBalance and EndingTotal.
17 Dec 2009 - Beena - Removed unnecessary code
14 Dec 2009 - Abhay - Defect ID 16 (Live Release)- For showing Return Quantity and Avg Return value
30 Sep 2009 - Beena Shah -Average Rate were not calculated properly
4th Sep 2009 - Beena Shah - Defect 181 - Show Transfer as a separate column for inv activity
28/08/2009 Beena Shah Added Total Adjustment value and also UPDATEd proc for right calculations
16/06/2009 Beena Shah Added Adjustment qty for closing balance
** =========================================================================================*/
CREATE PROCEDURE usp_INV_get_InventoryActivity
(
@Company_Code type_company_code,
@IPart_No INT --Company Code For Labor code
)

AS Begin

DECLARE @Inv_Precision INT,
@Currency Nvarchar(10)



SELECT @Inv_Precision=Inv_SET_Inventory_Precision ,
@Currency = Currency_Symbol
FROM cd_General_parameter G inner join CD_Currency C
On G.Gen_country_Code = C.Country_Code
And G.Company_Code = C.Company_Code
WHERE G.Company_Code=@Company_Code


SELECT
company_code,
CurrentMonth=datename(mm,convert(nvarchar(2),IMonth)+'/01/2000'), -- To get month name datename function needs date in format 'mm/dd/yyyy'
CurrentMonthNumeric=isnull(IMonth,''),
CurrentYear =IYear,

--opening qty
OpeningBalance = ISNULL(case when sum(OpeningQty) < 0 then 0 else sum(OpeningQty) end,0.0),

--Issue
IssuedQuantity= ISNULL(sum(issuedqty),0.0),
TotalIssuedValue= @Currency + ' ' + ISNULL(sum(TotalIssueValue),0.0) ,
AvgIssuedQuantity= @Currency + ' ' + STR(ISNULL(case when sum(issuedqty)=0 then Avg(AvgIssueRate)
else sum(TotalIssueValue)/sum(issuedqty)
end,0.0),30,@Inv_Precision),

--Receive
ReceivedQuantity=ISNULL(Sum(recqty),0.0),
TotalReceivedValue=@Currency + ' ' + STR(ISNULL(Sum(TotalRecValue),0.0),30,@Inv_Precision),
AvgReceivedQuantity=@Currency + ' ' + STR(ISNULL(case when sum(recqty)=0 then Avg(AvgRecRate)
else sum(TotalRecValue)/sum(recqty)
end,0.0),30,@Inv_Precision),

--Adjustment
AdjustmentQty=ISNULL(Sum(AdjustmentQty),0.0),
TotalAdjValue=@Currency + ' ' + STR(ISNULL(Sum(TotalAdjustmentValue),0.0),30,@Inv_Precision),
AvgAdjustmentRate=@Currency + ' ' + STR(ISNULL(case when sum(AdjustmentQty)=0 then Avg(AvgAdjustmentRate)
else sum(TotalAdjustmentValue)/sum(AdjustmentQty)
end,0.0),30,@Inv_Precision),

--Transfer
TransferedQty = ISNULL(sum( case When TransferedQty >= 0 then TransferedQty
else 0 end ),0.0),
TotalTransferValue=@Currency + ' ' + STR(ISNULL(sum(case When TransferedQty >= 0 then TotalTransferValue
else 0 end ),0.0),30,@Inv_Precision),
AvgTransferRate=@Currency + ' ' + STR(ISNULL(case when sum(case When TransferedQty >= 0 then TransferedQty
else 0 end )=0 then Avg(AvgTransferRate)
else sum(case When TransferedQty >= 0 then TotalTransferValue
else 0 end )/sum(case When TransferedQty >= 0 then Transfere
hypermellow 23-May-14 9:22am    
Without seeing your data it's hard see where the problem is.
The error message "Error converting data type nvarchar to numeric." is telling you, that you are evaluating a value as a number, when it is not.
The error should give you a line number in your procedure where this happened.

If you try commenting out the columns that use the STR function (i.e. - TotalAdjValue,AvgAdjustmentRate,TotalTransferValue etc) one at a time, then you will know where to start looking.

Another suggestion would be to wrap ISNULL statements around dependant items that are use in calculated columns.

i.e. - Adding:
ISNULL(TransferedQty,0) >= 0
to:
TotalTransferValue=@Currency + ' ' + STR(ISNULL(sum(case When TransferedQty >= 0 then TotalTransferValue
else 0 end ),0.0),30,@Inv_Precision),

Hope it helps.

Typically this error happens when there are non-numeric values stored in the database. Your best bet is to reduce the query to a SIMPLE query: Select x from TABLE order by x; where you still get the error. I have seen spaces or just badly formatted numbers: ".0" or "1e5" values cause this kind of error message. IF it is only the order by that fails (which I do not see above, hence requesting a simpler query), sometimes you can code it as order by to_number(regex_replace(to_string(value))) -- to get it down to just numbers and confirm it is bad characters. THEN from there, you can create the WHERE clause to find the data causing the problem, and figure out the best way to deal with it.
 
Share this answer
 
i think you are try to concat the currency with the numeric data
this is a simple front end job, just create the currency as a separate column, and order the numeric data as you want.
in front end just join the currency with the required columns
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900