Fetching SAP table data throws Error 8114 - sql-server

I'm trying to update SQL data from SAP data (same happens with insert). I'm always getting runtime error
SQL error 8114
The reason for the exception is:
Database error text: "Error converting data type nvarchar to numeric."
All fields of 'gs_sap_table2' are type CHAR100. I've tried many things like converting some fields of my structure from CHAR 100 to decimal 18,5 and fill all the 'NOT NULL' SQL fields with values but problem remains.
I'm posting you my ABAP code and a picture of the SQL database fields:
EXEC SQL.
CONNECT TO :gv_connection
ENDEXEC.
EXEC SQL.
UPDATE STOCKKEEPINGUNITS
SET ProductId = :GS_SAP_TABLE2-PRODUCTID,
CreatedOn = :GS_SAP_TABLE2-CREATEDON,
UpdatedOn = :GS_SAP_TABLE2-UPDATEDON,
UPC = :GS_SAP_TABLE2-UPC,
AvailabilityType = :GS_SAP_TABLE2-AVAILABILITYTYPE,
Stock = :GS_SAP_TABLE2-STOCK,
Currency = :GS_SAP_TABLE2-CURRENCY,
TaxClass = :GS_SAP_TABLE2-TAXCLASS,
RetailCurrentPrice = :GS_SAP_TABLE2-RETAILCURRPRICE,
Weight = :GS_SAP_TABLE2-WEIGHT,
MeasurementUnitId = :GS_SAP_TABLE2-MEASUREMENTUID,
NameL1 = :GS_SAP_TABLE2-NAMEL1,
NameL2 = :GS_SAP_TABLE2-NAMEL2,
ShippingCost = :GS_SAP_TABLE2-SHIPPINGCOST
WHERE SKUId = :GS_SAP_TABLE2-SKUID
ENDEXEC.
EXEC SQL.
COMMIT
ENDEXEC.
EXEC SQL.
DISCONNECT :gv_connection
ENDEXEC.

The error tells you that there is an nvarchar value and it is being attempted to be implicitly converted into a numeric type. So, the error happens either in the SET or the WHERE clause. Since in the WHERE clause from the two operands one is the field, SKUId, which is a varchar and not an nvarchar nor a numeric type, it is clear that the error happens somewhere in the SET clause.
In the SET clause you assign values to fields. Conversion from nvarchar to numeric happens in these assignments if and only if the right-hand-side (i.e. the value) is nvarchar and the left-hand-side (i.e. the field) is numeric.
So, in order to fix your issue, you will need to
create a list for yourself containing all numeric fields in the SET clause that receive some value
make sure that the value (right-hand-side) of the assignment operation for each field is converted into a numeric value of the exact type your field expects

Related

SQL Server : error converting data type nvarchar to numeric

I am new to this and I am trying to learn more about SQL. I have this example and still not able to figure it out why I am getting this error:
DECLARE #xmlprice XML = '<price>1234567</price>'
DECLARE #price DECIMAL(19,4);
SET #price = #xmlprice.value('sum(price)', 'DECIMAL(19,4)');
I get the following error:
Error converting data type nvarchar to numeric.
I think that's a bug, but it's such a strange thing to do I'm not sure. Normally you would project the XML values out first, and then aggregate them.
eg
select #xmlprice.value('(/price)[1]','decimal(19,4)');
This expression says to start at the root '/' and then find all the 'price' elements, pick the first one, and extract its value.
You may want to post this on the SQL Server feedback site for the product team to evaluate.
Based on your recommendation, I cast my price value to decimal with a FLWOR Statement and Iteration (XQuery) before the sum.
DECLARE #xmlprice XML = '<price>1234567</price>'
DECLARE #price DECIMAL(19,4);
SET #price = #xmlprice.value('sum(for $price in //price return $price cast as xs:decimal?)', 'DECIMAL(19,4)');

Conversion failed when converting the varchar value \'08/01/1979\' to data type int

Am trying to fetch data from a SQL database on the basis of date using where clause. My SQL query is
select DISTINCT Name from [DataBase] Where DOB ="+date
I want to change the date variable dynamically but am running into this error.
Conversion failed when converting the varchar value \'08/01/1979\' to data type int.
I'm using this query in my node.js project.
I tried using Date object but my database date is in varchar so its not working either. I tried the solutions available in stackoverflow but none worked for me.
Conversion failed when converting the varchar value 'none' to data type int

Conversion failed when using nvarchar, but not varchar

I've run into a situation where I'm getting an error if I run the same query on the same table, the difference being the column type. The query used is:
SELECT [name] FROM [demo] WHERE [name] = 1111111
If the name column is varchar(7) then it runs no problem.
If the name column is nvarchar(7) then it gives an error:
Conversion failed when converting the nvarchar value 'BBBBBBB' to data type int.
The error makes sense - I can see why the integer value can't be directly compared to the string value. I also realise that I can resolve it with a cast to a string for the condition.
However, what I'm not getting is why the behaviour is different for nvarchar and varchar. This chart appears to suggest that they should have the same behaviour for type conversion.
Does anyone know why this is happening?
If you using nvarchar column your query should look like:
SELECT [name] FROM [demo] WHERE [name] = N'1111111'
N at the begin of string to make sure that your string is unicode string (nvarchar)

how to check for a null value in t-sql

I want to check for a value in a table to see if it's null.
If so, I just want output to be '0.000'; otherwise, I'd like the actual value which must be converted to a varchar field.
The definition of the column in the database is decimal(10,3).
When I run my below query (pertinent part of the query), I get an error message:
Error converting data type varchar to numeric.
Here is my t-sql syntax that is getting the error. How do I correct it?
isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
EDIT
Here is the full SELECT statement:
Select '4'+','
+','+Case when Country_Code= 1 then 'USA' else 'CAN' End
+','+
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+
+','+'01/01/1900'
+','+'01/01/2099'PartMaster_String
,4 ord,part_no
, RIGHT('000'+convert(varchar(3),ATA_SYSCode),3)+'-'+RIGHT('000'+convert(varchar(3),ATA_Asmcode),3)+'-'+RIGHT('000'+convert(varchar(3),ATA_Partcode),3) taskstring
From tbl_CPM_PARTS_MASTER_PTC
The reason for the error message is that it is possible for the line you posted to simply have Ryder_Price as a value, which is a decimal type. And then you are trying to concatenate it to a string.
To acheive your stated goal:
I want to check for a value in a table to see if it's null.
If so, I just want output to be '0.000'; otherwise, I'd like the
actual value which must be converted to a varchar field.
Try this:
convert(varchar(max),isnull(Ryder_Price,'0.000'))

Encountering SQL Error not having to do with my query... what can I do to get to my data?

I'm trying to run this query:
select * from activity where id = 9348927
And I receive this error:
Conversion failed when converting the varchar value '04 30' to data type int.
Note that I still receive this error when I change * to id, which doesn't make any sense to me. I don't receive the error when I run this:
select top 32 * from activity where id = 9348927
I'm pretty sure this means that some of the data on the 33rd row is in a format that SQL Server doesn't like. Is there any way I can get it to ignore the error and display my data anyway?
EDIT: id is varchar(10)
This usually means that the id column is not a number datatype, but varchar.
9348927 is a number (int) and datatype precedence means the the string value '04 30' is converted to int. Standard SQL behaviour.
Try this:
select * from activity where id = '9348927'
Incidently, the implicit conversion means any index on id will not be used. Compare query plans.
The best way to "work around" this is to fix your data and column type. Otherwise, do the comparison as string which is what id most likely is.

Resources