Dapper.Net SQL bulk insert decimal precision - dapper

I am getting an exception from Dapper Bulk Copy
Looks like the underlying bulk copy operation is failing. I dumped the data in json and found the value creating problem is 259815703.3430760631
StackTrace:
at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed)
.....
Inner Exception 1:
InvalidOperationException: The given value of type Decimal from the data source
cannot be converted to type decimal of the specified target column.
Inner Exception 2:
ArgumentException: Parameter value '259815703.34307606' is out of range
The table has decimal(18,6) and instead of storing with lower precision the API is throwing.
I tried the following and it works, the value that is stored is 6 decimal instead of 10 as expected
CREATE TABLE #t1(c1 DECIMAL(18,6))
INSERT INTO #t1(c1) values(259815703.3430760631)

I solved the issue with decimal.Round to 6 places of decimal

Related

Fetching SAP table data throws Error 8114

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

Error converting data type nvarchar to float in SQL Server 2008

select sum(cast(mmax as float)
from table
mmax is of datatype nvarchar and the value is
string,int,decimal, value
I trying to sum of like value 17.50,35.00.
I am avoiding string value in where clause
But not solved this problem
Error is thrown
String/Varchar values with commas such as "10,000" pass the IsNumeric() test but do not cast/convert into numeric types without raising an error.
You can replace the commas and perform the cast and sum operation:
select sum(cast(replace(mmax,',','') as float))
from tbl
where isnumeric(maxx)>0
One of the values cannot be converted to a float. You may have a million values that can convert if one (has a letter O instead of a 0 for example) you will get that message.

T-SQL converting string into float

In a stored procedure on my SQL Server, I am trying to convert values from a varchar column into a float format.
The values into the varchar column are numbers with a sign at the beginning and a '.' before decimals.
Examples: '+0000000000000044.09' or '-0000000000114995.61'
If I try this: convert(float,mystring), it doesn't work.
I have:
Error converting data type varchar to float
Is this kind of conversion possible?
Or is there another way to convert a string with a sign and a '.' into a float?
As your examples both work, I'd guess there's another value somewhere in your table that's causing the problem. In recent versions of SQL Server (2012 onwards), the TRY_CONVERT function can be useful for tracking down this kind of issue.
TRY_CONVERT will not throw an exception on a conversion failure, but instead return a NULL value, so you can figure out which values are causing the problem like this:
SELECT * FROM your_table WHERE TRY_CONVERT(FLOAT, your_column_name) IS NULL
If any rows are returned, those are the rows with problem values that can't be converted to FLOAT.
You can try using CAST(mystring as float) or TRY_CONVERT(float,mystring).
Thuough convert(float,mystring) also should work fine. I would suggest checking your data.

Tinyint not as Boolean

I have an existing mysql database which I want to query with scalikejdbc.
The database uses a nullable tinyint for small values in the range [-1,3].
How can I query this value? The only way that seems to work is treating it as a boolean which does not reflect all the values.
sql"SELECT * FROM table"
.map(rs => rs.nullableBoolean("value"))).list().apply
If I try to treat it as byte or int or I get a Runtime Exception
[ResultSetExtractorException: Failed to retrieve value because For input string: "true". If you're using SQLInterpolation, you may mistake u.id for u.resultName.id.]

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